How to send email with PHP?

Sending emails with PHP can be done with built-in mail function available in PHP core. Building modern contact forms, customizing headers, sending HTML emails, SMTP sending, local development, testing emails and other advanced functionalities are sort of a must these days. That is why using libraries that can help you get up to speed with emailing in PHP is recommended. There are many competing open source libraries for sending emails with PHP available:


Let’s send simple email with built-in mail function:


$message = 'Hello, world.';

mail('', 'My subject', $message);

Keep in mind that for above to work you will need to setup also mail server.


Let’s look at a simple example to use PHPMailer and SMTP:


require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = ';';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = '';
$mail->FromName = 'Mailer';
$mail->addAddress('', 'Joe User');     // Add a recipient
$mail->addAddress('');               // Name is optional
$mail->addReplyTo('', 'Information');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';

Swift Mailer

Let’s take the above example and refactor it to use the Swift Mailer library:


require_once 'lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('', 25)
    ->setUsername('your username')
    ->setPassword('your password')

You could alternatively use a different transport such as Sendmail or Mail:

// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

// Mail
$transport = Swift_MailTransport::newInstance();

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
    ->setFrom(['' => 'John Doe'])
    ->setTo(['', '' => 'A name'])
    ->setBody('Here is the message itself')

// Send the message
$result = $mailer->send($message);

Zend Mail

In the following example we assume you know Zend Framework. Let’s send an email with Zend Mail:

use Zend\Mail;

$mail = new Mail\Message();
$mail->setBody('This is the text of the email.');
$mail->setFrom('', 'Sender\'s name');
$mail->addTo('', 'Name of recipient');

$transport = new Mail\Transport\Sendmail();

See also

Some other useful 3rd party services to check out when sending emails:

GitHub OctocatFound a typo? Something wrong with this content? Just fork and edit it.

Content of this work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. Code snippets in examples are published under the CC0 1.0 Universal (CC0 1.0). Thanks to all the contributors.