Sending Email in PHP

Most of your interaction with users will be via web pages, but sending or receiving an email message every now and then is useful, too. Email is a great way send updates, order confirmations, and links that let users reset their passwords.

This chapter explains the basics of using the Swift Mailer library to send email messages.

1. Swift Mailer

First, use Composer to install Swift Mailer:

php composer.phar require swiftmailer/swiftmailer

As long as you’ve got the standard require “vendor/autoload.php”; statement in your program, Swift Mailer is now available to use.

Swift Mailer represents messages as Swift_Message objects. To create an email mes­sage, you create one of these objects and then call methods on it to build the message. Then you hand the message object to an instance of the Swift_Mailer class so that the message can be sent. The Swift_Mailer instance, in turn, is configured with a particular kind of Swift_Transport class. This transport class embodies the logic of how the message is actually sent—either by connecting to a remote server or by using mail utilities on the local server.

Example 17-1 creates a simple email message with a subject, from address, to address, and plain-text body.

Example 17-1. Creating an email message

$message = Swift_Message::newInstance();

$message->setFrom(‘julia@example.com’);

$message->setTo(array(‘james@example.com’ => ‘James Bard’));

$message->setSubject(‘Delicious New Recipe’);

$message->setBody(<<<_TEXT_

Dear James,

You should try this: puree 1 pound of chicken with two pounds of asparagus in the blender, then drop small balls of the mixture into a deep fryer. Yummy!

Love,

Julia

_TEXT_

);

The arguments to setFrom() and setTo() can be email addresses as strings or email addresses and full names as key/value pairs. To specify multiple recipients, pass an array that contains any mix of strings (addresses) and key/value pairs (addresses and full names).

The setBody() method sets the plain-text body of the message. To add an HTML version of the message body, call addPart() with the alternative message body as a first argument and the right MIME type as the second argument. For example:

$message->addPart(<<<_HTML_

<p>Dear James,</p>

<p>You should try this:</p>

<ul>

<li>puree 1 pound of chicken with two pounds of asparagus in the blender</li> <li>drop small balls of the mixture into a deep fryer.</li>

</ul>

<p><em>Yummy!</em</p>

<p>Love,</p>

<p>Julia</p>

_HTML_

// MIME type as second argument , “text/html”);

A message needs a mailer to send it, and a mailer needs a transport to know how to send the message. This chapter shows the Simple Mail Transfer Protocol (SMTP) transport, which connects to a standard email server to send the message. Creating an instance of the Swift_SmtpTransport class requires you to know the hostname and port (and maybe username and password) of your mail server. Ask your system administrator or check the account settings in your email program to find this infor­mation.

Example 17-2 creates a Swift_SmtpTransport object that uses an example SMTP server at port 25 of host sntp.exanple.com and then creates a Swift_Mailer object which uses that transport.

Example 17-2. Creating an SMTP transport

$transport = Swift_SmtpTransport::newInstance(‘smtp.example.com’, ; );

Smaller = Swift_Mailer::newInstance($transport);

Once you’ve got a Swift_Mailer, pass your Swift_Message object to its send() method and your message is sent:

$mailer->send($message);

Swift Mailer supports a lot more functionality than what’s described here. You can attach files to messages, add arbitrary headers to messages, request read receipts, connect to mail servers over SSL, and more. The Swift Mailer documentation is the place to start to find out the details of these features.

Source: Sklar David (2016), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.

Leave a Reply

Your email address will not be published. Required fields are marked *