Configuring Drupal to Use an External SMTP Server

drupal-smtp

drupal-smtp Many of you Windows folks who are already running Drupal already know how to use SMTP with Drupal…mostly because Windows hosted servers typically don’t run Sendmail.   So why Tony, why, even though you are running a Linux box, Drupal and sendmail, do you want to use an external SMTP server?

Simple…it seems that not all of my emails get delivered.  Why is this? You know how when you create a contact form in PHP and you have to Spoof the headers to say the email is coming from that person…even though it is not? Well, a ton of spam destroying boxes out there don’t really like that.  In fact, chances are PLENTY of your emails being sent are not evening making it to the intended recipient.

What I really need is a reliable email delivery service that I can extend off of Drupal so i know that my emails are being delivered. I ran across (thanks to a good buddy of mine) DNS Exit with provides a slick mail relay outbound service. *The service can be also used as Smart Host for your MS Exchange server that having problems to send emails to AOL/Yahoo/Hotmail addresses.

So anyways, the service cost some money, but it is worth it in the end if you want to be sure that your emails are getting delivered.

So now that is ready to go, the next step is to turn on SMTP Authentication Support in Drupal.  The module allows Drupal to bypass the PHP mail() function (and also doesn’t require you to configure sendmail to use SMTP instead of sendmail).  This module requires you to also grab a copy of Phpmailer, which contains all of the classes needed to send an smtp message.  All of the instructions are in the readme file…but as an overview here is what you have to do:

1.  Copy the files included in the tarball into a directory named "smtp" in
    your Drupal sites/all/modules/ directory.
2.  Download the PHPMailer package (the URL is listed in REQUIREMENTS section f the README.txt file) and place it in a directory named "phpmailer" in
    your <drupal-root>/sites/all/modules/smtp directory.
3.  Login as site administrator.
4.  Enable the SMTP Authentication Support module on the Administer -> Site
    building -> Modules page.
5.  Fill in required settings on the Administer -> Site configuration -> SMTP
    Authentication Support page.

That is pretty much it.  Now that will cover all core emailing tools, but you know how Drupal is…so easy to let you write your own PHP code.  You may be using a custom form that doesn’t use the core Drupal mailing routines.  If that is the case, well I’ve come to your rescue.  Below is a quick and dirty SMTP mail doo-hickey (some call it a mailing class…this one is from Sendmail:

function SendMail($ToName, $ToEmail, $FromName, $FromEmail, $Subject, $Body, $Header)
{
$SMTP = fsockopen("yourdnsrelayprovider.com", 25); <–Sometimes port 25 is blocked

$InputBuffer = fgets($SMTP, 1024);

fputs($SMTP, "HELO mydomain.com\n");
$InputBuffer = fgets($SMTP, 1024);
fputs($SMTP, "MAIL From: $FromEmail\n");
$InputBuffer = fgets($SMTP, 1024);
fputs($SMTP, "RCPT To: $ToEmail\n");
$InputBuffer = fgets($SMTP, 1024);
fputs($SMTP, "DATA\n");
$InputBuffer = fgets($SMTP, 1024);
fputs($SMTP, "$Header");
fputs($SMTP, "From: $FromName <$FromEmail>\n");
fputs($SMTP, "To: $ToName <$ToEmail>\n");
fputs($SMTP, "Subject: $Subject\n\n");
fputs($SMTP, "$Body\r\n.\r\n");
fputs($SMTP, "QUIT\n");
$InputBuffer = fgets($SMTP, 1024);
fclose($SMTP);
}
?>

So now, all you have to do for it to work is call the function like this:

$ToName="To Name";
$ToEmail="[email protected]";
$FromName = "My Name is…";
$FromEmail = "[email protected]";
$Subject = "I’m testing emails with PHP";
$Body = "Hi Strongbad, this is just a test message.";
echo $ToEmail;

SendMail($ToName, $ToEmail, $FromName, $FromEmail, $Subject, $Body, $Header);

1 thought on “Configuring Drupal to Use an External SMTP Server”

  1. Thanks, we got the solution. But there is one hint, when u add new module give the permission to that module. Until the permission allow it is not activated.

Leave a Reply

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