Log In

Step 1

The stock mail() that comes with PHP does not usually allow you to use an SMTP server of your choice, and it does not support SMTP authentication. The free PEAR Mail package fixes the above problems.

Firstly, make sure PEAR is installed. If you are unsure if Pear is installed, type pear and press Enter at the command prompt. You’ll see a list of pear commands if it is already installed.

$ pear
Commands:
build Build an Extension From C Source
bundle Unpacks a Pecl Package
channel-add Add a Channel
...

Step 2

Typically, in particular with PHP 4 or later, PEAR is already installed for you. If it is not installed, You can download and install as per the instructions provided here.

Step 3

Install PEAR Mail package and its dependencies as given below:

$ pear install Mail
$ pear install Net_SMTP
$ pear install Net_Socket
$ pear install Mail_Mime

Step 4

  • Adjust the example below for your needs. Make sure you change the following variables:
    • from: the sender’s email address and name.
    • to: the recipient’s email address and name.
    • username: your SMTP username.
    • password: your SMTP password.
    • host: mail.smtp2go.com.
<?php
require_once "Mail.php";
require_once "Mail/mime.php";

$from = "Susan Sender <sender@example.com>";
$to = "Rachel Recipient <recipient@example.com>";
$subject = "Hi!";
$text = "Hi, It is a text message";
$html = "It is a HTML message";
$mime = new Mail_mime();
$mime->setHTMLBody($html);
$mime->setTXTBody($text);
$body = $mime->get();

$host = "mail.smtp2go.com";
$port = "2525"; // 8025, 587 and 25 can also be used.
$username = "smtp_username";
$password = "smtp_password";

$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$headers = $mime->headers($headers);
$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("

" . $mail->getMessage() . "

");
} else {
echo("

Message successfully sent!

");
}
?>

Step 5

PHP Script to Send Email Using SMTP Authentication and SSL Encryption:

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

$from = "Susan Sender <sender@example.com>";
$to = "Rachel Recipient <recipient@example.com>";
$subject = "Hi!";
$text = "Hi, It is a text message";
$html = "It is a HTML message";
$mime = new Mail_mime();
$mime->setHTMLBody($html);
$mime->setTXTBody($text);
$body = $mime->get();

$host = "ssl://mail.smtp2go.com";
$port = "465";
// 8465 can also be used.

$username = "smtp_username";
$password = "smtp_password";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$headers = $mime->headers($headers);
$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("

" . $mail->getMessage() . "

");
 } else {
  echo("

Message successfully sent!

");
 }
?>

Please note: If you experience “timeouts” or problems connecting due to an unreliable internet connection, consider increasing the max_execution_time setting in the php.ini file on your server, to a value such as 120 (instead of the default value 30).

Ready for better email delivery?

Try SMTP2GO free for as long as you like:

Try SMTP2GO Free → Paid plans available for over 1,000 emails/month.