PHP: Main
The mail function is used to send E-mail Messages through the SMTP server specified in the php.ini Configuration file. Thus, if it is wrong configured, it will not work. Here, you won't learn how to make a client which will also accept, but it's doable. You can help yourself on PEAR and PECL Repository (someone should make an Tutorial how to install/extend PHP libraries) .
PHP's mail() function works thus:
bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])
It is important for you to understand that it returns a boolean true/false on success or failure, respectively.
This example will send message "message" with the subject "subject" to email address "example@domain.com". Also, the receiver will see that the eMail was sent from "Example2 <example2@domain.com>" and the receiver should reply to "Example3 <example3@domain.com>"
<?php
mail(
"example@domain.tld", // E-Mail address
"subject", // Subject
"message", // Message
"From: Example2 <example2@domain.com>\r\nReply-to: Example3 <example3@domain.com>) // Additional Headers
;
?>
These headers are optional though.
Also, there is no requirement to write E-mail addresses in format "Name <email>", you can just write "email".
This will send the same message as the first example but includes From: and Reply-To: headers in the message. This is required if you want the person you sent the E-mail to reply to you. Also, some E-mail providers will assume mail is spam if certain headers are missing so unless you include the correct headers you mail will end up in the junk mail folder.
Keep in mind, though, that by default mail() sends plain text message, so to make a HTML message, you should use this headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Note: \r\n is actually \n, a newline, but you must use \r\n to keep compatibility.
And that's it! To make something like newsletter, all you need is to run a loop and use mail() .
Much love