SMTP new

Sending email with PHP from SMTP server

Posted on Posted in PHP, Technology

For mailing through PHP, Sometimes we need to use an external server to send emails using the details which includes server host, port, username, password, encryption type, etc.

For doing this you need to make connection to the external server using the fsockopen function and authenticate to send the email to the recipient.

Most easily and useful alternative of this is that you can use the PHPMailer class which already does the authentication for you using the credentials you provide to it and send an smtp email using the object the class provides you.

You can download the latest PHPMailer library from here and below is an example of the sample code snippet which is pretty much self explanatory.

&lt;br /&gt;<br />
&amp;lt;?php&lt;br /&gt;<br />
require 'PHPMailerAutoload.php';&lt;/p&gt;<br />
&lt;p&gt;$mail = new PHPMailer;&lt;/p&gt;<br />
&lt;p&gt;//$mail-&amp;gt;SMTPDebug = 3;                               // Enable verbose debug output&lt;/p&gt;<br />
&lt;p&gt;$mail-&amp;gt;isSMTP();                                      // Set mailer to use SMTP&lt;br /&gt;<br />
$mail-&amp;gt;Host = ';';  // Specify main and backup SMTP servers&lt;br /&gt;<br />
$mail-&amp;gt;SMTPAuth = true;                               // Enable SMTP authentication&lt;br /&gt;<br />
$mail-&amp;gt;Username = '';                 // SMTP username&lt;br /&gt;<br />
$mail-&amp;gt;Password = 'secret';                           // SMTP password&lt;br /&gt;<br />
$mail-&amp;gt;SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted&lt;br /&gt;<br />
$mail-&amp;gt;Port = 587;                                    // TCP port to connect to&lt;/p&gt;<br />
&lt;p&gt;$mail-&amp;gt;From = '';&lt;br /&gt;<br />
$mail-&amp;gt;FromName = 'Mailer';&lt;br /&gt;<br />
$mail-&amp;gt;addAddress('', 'Joe User');     // Add a recipient&lt;br /&gt;<br />
$mail-&amp;gt;addAddress('');               // Name is optional&lt;br /&gt;<br />
$mail-&amp;gt;addReplyTo('', 'Information');&lt;br /&gt;<br />
$mail-&amp;gt;addCC('');&lt;br /&gt;<br />
$mail-&amp;gt;addBCC('');&lt;/p&gt;<br />
&lt;p&gt;$mail-&amp;gt;addAttachment('/var/tmp/file.tar.gz');         // Add attachments&lt;br /&gt;<br />
$mail-&amp;gt;addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name&lt;br /&gt;<br />
$mail-&amp;gt;isHTML(true);                                  // Set email format to HTML&lt;/p&gt;<br />
&lt;p&gt;$mail-&amp;gt;Subject = 'Here is the subject';&lt;br /&gt;<br />
$mail-&amp;gt;Body    = 'This is the HTML message body &amp;lt;b&amp;gt;in bold!&amp;lt;/b&amp;gt;';&lt;br /&gt;<br />
$mail-&amp;gt;AltBody = 'This is the body in plain text for non-HTML mail clients';&lt;/p&gt;<br />
&lt;p&gt;if(!$mail-&amp;gt;send()) {&lt;br /&gt;<br />
echo 'Message could not be sent.';&lt;br /&gt;<br />
echo 'Mailer Error: ' . $mail-&amp;gt;ErrorInfo;&lt;br /&gt;<br />
} else {&lt;br /&gt;<br />
echo 'Message has been sent';&lt;br /&gt;<br />
}&lt;br /&gt;<br />

As you can see in the code that once the object is created the below code is added just to let the library know that this is an smtp request.


You need to provide the host, username, password and the port number in-order to get authenticated to the remote server.

By defult an smtp port number is 25 but this also varies based on the server you are using, let the smtpauth be true so that the authentication is successfully done.

Once you’re credentials are updated in the script you can use it to send email from any application.