Help Docs Email Testing and Sending Mail Using PHP

Testing and Sending Mail Using PHP

If you are familiar with PHP, you can create PHP scripts to send mail. PHP mail provides incredible control of the data and content of the emails sent from your domain or site. However, PHP mail only works when relayed over SMTP. The following article shows you how to test and send different types of email using PHP.

Note:

The PHP code provided below is only an example. Your specific website may require different code. As outlined in our article Management and Support Levels, we are unable to troubleshoot code issues. We recommend speaking with a developer before implementing any PHP-based email functionality.

If you are familiar with PHP, you can create PHP scripts to send mail. PHP mail provides incredible control of the data and content of the emails sent from your domain or site. However, PHP mail only works when relayed over SMTP. The following article shows you how to test and send different types of email using PHP. 

Note:

We recommend using an SMTP relay that requires authentication. Sending mail through unauthenticated SMTP servers (including the localhost relay on Cloud Sites) can result in delays or undelivered email because of stringent anti-spam filters.

Testing PHP Mail Functionality

Before you can send PHP mail, it important to test your PHP mail script. Your developer will know the best way to create and test your individual mail script. Your code might look like the following code (save this into a file with a .php extension):

<?php
 $headers = 'From: webmaster@example.com';
 mail('nobody@example.com', 'Test email using PHP', 'This is a test email message', $headers, '-fwebmaster@example.com');
?>
Here is another test script you might use:

<?php
$to = 'nobody@example.com';
$subject = 'Test email using PHP';
$message = 'This is a test email message';
$headers = 'From: webmaster@example.com' . "rn" . 'Reply-To: webmaster@example.com' . "rn" . 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers, '-fwebmaster@example.com');
?>

Although you can send email just by using these types of scripts, we do not recommend it. Without SMTP authentication (in other words, using a specific mail server to send your email), your email will likely not be delivered.

Testing PHP SMTP Functionality

We highly recommend you use this method when sending email via PHP. Using SMTP authentication means you are connecting to an external email server to send email. Your email is more likely to be delivered because receiving servers see your email as trustworthy.

You can test PHP SMTP functions with the following examples. The first one is standard SMTP, and the second one is SMTP with SSL.

This code is just provided as an example, and the host, username, and password values depend on the provider that you’re using to send your email.

Mail.php is a PEAR module, and must be installed on your server and enabled to use PHP mail scripts. It is included in the default include_path for PHP, so requiring it in these scripts works by default. Your developer will be able to find the necessary settings and write code to accommodate your mail service.

  • If you have a legacy Cloud Sites email account or are using Rackspace Email, SSL/TLS is required. The host is secure.emailsrvr.com, the username is your email address and the port is 465.
  • If you’re using a third party service such as SendGrid or Gmail to send email, you must provide the SMTP server for that service.

Sending Mail with PHP SMTP

For the following variables, replace the example with the appropriate information from your system:

  • $from
  • $to
  • $subject
  • $body
  • $host
  • $username
  • $password

The example values will be highlighted in red in the sample script.

Here is a sample PHP script for sending email via standard SMTP. Your website may need a slightly different script. Your developer will be able to create the right script for your website:

<?php
require_once "Mail.php";

$from = "Web Master <webmaster@example.com>";
$to = "Nobody <nobody@example.com>";
$subject = "Test email using PHP SMTP";
$body = "This is a test email message";

$host = "SMTPhostname";
$username = "webmaster@example.com";
$password = "yourPassword";
$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>Message successfully sent!</p>");
}
?>

Sending mail with PHP SMTP Using an SSL

For the following variables, replace the example with the appropriate information from your system:

  • $from
  • $to
  • $subject
  • $body
  • $host
  • $username
  • $password
  • $port

The example values will be highlighted in red in the sample script.

Following is a sample PHP script for sending email via SMTP with SSL. Consult your developer for the code that will work best for you:

<?php
require_once "Mail.php";

$from = "Web Master <webmaster@example.com>";
$to = "Nobody <nobody@example.com>";
$subject = "Test email using PHP SMTP with SSL";
$body = "This is a test email message";

$host = "ssl://SMTPhostname";
$port = "465";
$username = "webmaster@example.com";
$password = "yourPassword";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>Message successfully sent!</p>");
}
?>

Send HTML Email via PHP

You can craft an HTML formatted email message when using SMTP authentication for delivery by making use of the built in Mail_Mime function provided by the PHP PEAR framework.

For the following variables, replace the example with the appropriate information from your system:

  • $from
  • $to
  • $subject
  • $text
  • $html
  • $file
  • $mimetype
  • $host
  • $username
  • $password

The example values will also be highlighted in red in the sample script.

You must supply a valid SMTP hostname and user credentials for authentication. If you are using a third-party mail service, replace mail.mailserver.com with the appropriate SMTP server hostname relative to the service. You can also attach files from your file system to the message if you provide a valid MIME type definition.

Here is a sample script:

<?php
require_once "Mail.php";
require_once "Mail/mime.php";

// see http://pear.php.net/manual/en/package.mail.mail-mime.php for further extended documentation on Mail_Mime

$from = "Web Master <webmaster@example.com>";
$to = "Nobody <nobody@example.com>";
$subject = "Test HTML email using PHP Pear w/ SMTP";
$text = "This is a text test email message";
$html = "<html><body><p>This is an html test email message
  <a href="http://www.example.com">This Is A Link</a></p></body></html>";
$crlf = "n";

// create a new Mail_Mime for use
$mime = new Mail_mime($crlf);
// define body for Text only receipt
$mime->setTXTBody($text);
// define body for HTML capable recipients
$mime->setHTMLBody($html);

// specify a file to attach below, relative to the script's location if not using an attachment, comment these lines out set appropriate MIME type for attachment you are using below, if applicable for reference see http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

$file = "attachment.jpg";
$mimetype = "image/jpeg";
$mime->addAttachment($file, $mimetype);

// specify the SMTP server credentials to be used for delivery if using a third party mail service, be sure to use their hostname
$host = "mail.mailserver.com";
$username = "webmaster@example.com";
$password = "yourPassword";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));


$body = $mime->get();
$headers = $mime->headers($headers);

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("
  " . $mail->getMessage() . "
  ");
} else {
  echo("
  Message successfully sent!
  ");
}
?>
Was this article helpful?