00 Votes

PHP: Sending an E-Mail

Tutorial by Stefan Trost | Last update on 2023-01-19 | Created on 2015-02-01

In this tutorial, I would like to show how to send a mail using PHP. For this purpose, we are using the PHP function mail().

First, we look at a very simple example for sending an email and then we see how we can send emails to several recipients, how we can write multi-line emails, how we can influence the sender, CC and BCC and how to send HTML emails.

How to send a simple E-Mail

Before caring about header and other details, we will look at how to write just a simple text mail to one recipient:

<?php
  mail('recipient@example.com', 'Subject', 'Hello');
?>

With this line of code, we are sending the message "Hello" to the email-address "recipient@example.com". As subject, we are using the word "Subject". So, as a first parameter, the function mail() is taking the recipient(s), as a second parameter the subject of the mail and the actual message can be passed as third parameter.

Multiple Recipients and Recipient-Names

In the first example, we are sending the email to only one recipient without specifying a name. If we would like to declare more than one recipient, we can just separate their addresses with a comma:

$recipients = 'recipient1@example.com, recipient2@example.com';

mail($recipients, 'Subject', 'Hello');

This email is sent to "recipient1@example.com" as well as to "recipient2@example.com".

Additionally, it is also possible to define names for the recipients which can be shown in the mail application:

$recipient = 'Name 1 <recp1@example.com>';
mail($recipient, 'Subject', 'Hello');
$recipients = 'Name 1 <recp1@example.com>, Name 2 <recp2@example.com>';
mail($recipients, 'Subject', 'Hello');

The example above shows how to realize this for one or two recipients. The e-mail address is just surrounded by < and > and the name is written before.

Multiline E-Mails

Using the characters "\r\n", we can insert a line break within the text of our mail:

$msg = 'Line 1' . "\r\n" . 'Line 2';
mail('recipient@example.com', 'Subject', $msg);
$msg = "Line 1\r\nLine 2";
mail('recipient@example.com', 'Subject', $msg);

It is important to always write "\r\n" within double quotes. Otherwise, the "\r\n" is not interpreted as newline.

Sender, CC and BCC

If we would like to define a special sender or if we would like to add optional CC or BCC recipients, we have to write this information into the header of our e-mail. The header can be passed as fourth, optional parameter to PHPs mail-function.

$header  = 'TO: Name <recipient@example.com>' . "\r\n";
$header .= 'FROM: Name <sender@example.com>' . "\r\n";
$header .= 'CC: ccrec@example.com' . "\r\n";
$header .= 'BCC: bccrec@example.com' . "\r\n";

mail('recipient@example.com', 'Subject', 'Hello', $header);

Of course, we can also omit the CC- and BCC-lines here to only specify the sender's and recipient's names.

HTML E-Mails

Up to now, in all examples, which has sent pure plain text e-mails. Finally, we would also have to look at how to write an HTML e-mail.

HTML e-mails can also be sent using the same mail function we have used before. The only difference is that we have to add additional information to the header, so that the main program knows that this mail should be interpreted as HTML mail.

Then, we can just write our message in HTML format the same way as building a web page.

$header  = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$header .= 'To: Recipient Name <recipient@example.com>' . "\r\n";
$header .= 'From: Sender Name <sender@example.com>' . "\r\n";

$msg = "
<html>
  <head>
    <title>Subject</title>
  </head>
  <body>
    <h1>Heading</h1>
    <p>Paragraph 1</p>
    <p>Paragraph 2</p>
  </body>
</html>
";

mail('recipient@example.com', 'Subject', $msg, $header);

The HTML part is specified as being in UTF-8 format with the header line about content type and charset. Unfortunately, this only refers to the mail text. It can still lead to problems when using Unicode characters such as accents or umlauts within the subject or the recipient name.

In this case, you should use the following way:

$sender  = '=?UTF-8?B?'.base64_encode('Martin Müller').'?=';
$header  = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$header .= 'From: ' . $sender . ' <s@example.com>' . "\r\n";

$subject = '=?UTF-8?B?'.base64_encode('Subject containing Ä, Ö, Ü').'?=';

$msg = "
<html>
  <head>
    <title>Subject containing Ä, Ö, Ü</title>
  </head>
  <body>
    <h1>Heading</h1>
    <p>Paragraph 1</p>
    <p>Paragraph 2</p>
  </body>
</html>
";

mail('recipient@example.com', 'Subject', $msg, $header);

Here, we are encoding the critical recipient- and subject-string using base64_encode(). This converts the UTF-8 Unicode string into a string without any 8 bit character, so that the string can be transferred without any problems. By surrounding the encoded part with "=?UTF-8?B?" and "?=" the mail application of the recipient knows how to handle and display this information.

You can find more detailed information about this procedure in my article about UTF-8 headers of e-mails.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

How to avoid Spam Mails

Tip | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.