When you need to send some emails on the web you can use the
mail() function of php. This function is pretty easy to understand and it allows you to add some extra header information to your emails. We are going to teach you how to make your emails much more organized but also clear and simple to understand.
The mail function
mail() function is really easy and it takes up to four parameters.
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
$to: The first parameter is the recipient's email
$subject: The subject is basically the title of the mail
$message: It is the content of the mail. It can be a simple text or a HTML code. It is better to use the php function wordwrap() if lines are longer than 70 characters
$additional_headers: Optional. The headers add some extra data to your emails like cc, from, priorityâ€¦ Multiple extra headers should be separated with a CRLF ( ), When sending mail, the mail must contain a From header
$additional_parameters: Optional. The additional_parameters parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail
The function itself returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise. It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
What headers are
The first three parameters are easy to understand, but what headers allow you to do? Headers are some extra data that you can add to your emails to make them more complete. We made a list of some useful header information which you can use in your emails:
'To: Lucas <email@example.com>, Andrew <firstname.lastname@example.org>': â€˜Toâ€™ defines the recipientsâ€™ names followed by their respective email.
'From: C.M.Programmers <email@example.com>': The â€˜Formâ€™ information is required, it defines the sender of the mail.
'Subject: C.M.Programmers mailâ€™: It is just like the $subject parameter.
'Cc: John@hotmail.it': â€˜Ccâ€™ stands for Carbon copy and it defines some extra recipients of the mail. â€˜Ccâ€™ will let the original recipient know who else has a copy of the message.
'Bcc: firstname.lastname@example.org': â€˜Bccâ€™ stands for Blind carbon copy and it works just like the â€˜Ccâ€™ but it hides these extra recipients from the original one.
'MIME-Version: 1.0': MIME stands for Multipurpose Internet Mail Extensions. It allows you to add support for some different encoding characters. You must add this header if you want to write the main message in HTML code.
'Content-type: text/html; charset=iso-8859-1': It defines the content type of the emailâ€™s message. You must add this header if you want to write the main message in HTML code.
'X-Mailer: PHP/' . phpversion(): It defines the php version of the mail sender.
'Reply-To: email@example.com': It is a 'reply-to' extra email address.
â€˜Importance: Highâ€™: These three headers define the priority of the mail. â€˜1â€™ is the highest value (most important) and â€˜5â€™ is the lowest. You probably donâ€™t need all these three headers together; you can use only the first one.
'Organization: C.M.Programmers': It defines the organization who sent the mail.
'Date: '. date('D, Y m d H:i:s'): It defines the date in which the email was sent. (In our example the date format is Day of the week, Year Month Day Hour Minutes Seconds).
'Message-ID: #uniqueIdentifierfirstname.lastname@example.org': Message-ID is a unique identifier for a digital message. Message-IDs, if present, are usually generated by the client program sending the email.
Header data example
To better understand what headers are take a look at this email data from Google Gmail:
In this image you can see all the extra header that you send with each email every day. Most of this information can be changed by setting the headers in your mail function.
PHP mail() example
Here you are an example on how to use the mail function to send a complete email. If you need to send multiple emails at once you can use a
while cycle. All the recipientsâ€™ mails may be stored on a MySql database. For each one of these emails you call the function to send a news to all your users.
// Multiple recipients $to = 'email@example.com, firstname.lastname@example.org'; // note the comma // Subject $subject = 'Mail example'; // Message $message = '#A simple text or an HTML code'; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0'; $headers = 'Content-type: text/html; charset=iso-8859-1'; // Additional headers $headers = 'To: Lucas <email@example.com>, Sally <firstname.lastname@example.org>'; $headers = 'From: C.M.Programmers <email@example.com>'; $headers = 'Subject: '. $subject; $headers = 'Cc: Andrew@hotmail.it'; $headers = 'Bcc: John@gmail.com'; $headers = 'X-Mailer: PHP/' . phpversion(); $headers = 'Reply-To: firstname.lastname@example.org'; $headers = 'X-Priority: 1'; $headers = 'Organization: C.M.Programmers'; $headers = 'Date: '. date('D, d M Y H:i:s'); $headers = 'Message-ID: #MessageIDemail@example.com'; $headers = 'X-Sender: firstname.lastname@example.org'; // Mail it if (mail($to, $subject, wordwrap($message, 70), implode("/ r/ n", $headers))) echo "SENDED"; else echo "NOT SENDED";
Php mail limitations
The mail() function itself has also some limitations. You may get this error while you try to send an email through php:
mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:#location.php on line XX
This appends because the mail command does not support authentication. Thatâ€™s why the
mail() function cannot be used in all cases.
Fortunately, there are some amazing libraries which can help you send emails with php. For example, you can use the most famous one, which is PhpMailer. We will write an article on how to use it soon, so stay tuned.
And speaking of emails, you should subscribe to our newsletter here below to receive an email every time we publish a new tutorial or article!