Using php mail(); and headers
So today, I came across an interesting question today which never had come up before to me. The PHP mail(); function. It is such a seemingly simple function, but often misunderstood; largely because its format and raw mail syntax.
PHP mail(); does not offer a specific field for each and every possible mail option, but we’ll go into it later. We’ll go through the very basics, then start touching upon what I am alluding to (headers).
The basic syntax is: mail(TO,SUBJECT,MESSAGE,HEADERS).
I assume one can figure out what each of those are (provided that you speak English well).
Now, since the function itself is self-explanatory, we’re back to the reason for this article; headers. Headers are optional fields in a CLR format using \r\n line-breaks between each of the different types. A common header would be something like from. For clarity (and cleanliness), headers are usually stored in a variable, so let’s take a quick look at how one code make this work.
<?php
$headers = "From: your@email.com\r\n
CC: a.public@email.address.com\r\n
BCC: secret@user1.com,\r\nsecret@user2.com,\r\nand@so.on.com\r\n";mail("someone@somewhere.com","Test e-mail!","Some message",$headers);
exit();
?>
The above code should thoroughly explain headers. There are many different fields one could put in there, but these are just a few. The above example includes a from address, a CC, and a BCC (with multiple addresses).
So overall, the php mail(); function is a simple, yet very useful tool for many users to use. A much better alternative to some forms, especially the last resort “mailto” link type.
Regards,
Dennis M.
Thanks for posting about this, I would like to read more about this topic.