You are viewing our old blog site. For latest posts, please visit us at the new space. Follow our publication there to stay updated with tech articles, tutorials, events & more.

Sending Mails Through SMTP Servers ( Gmail/Yahoo/Outlook )

0.00 avg. rating (0% score) - 0 votes
Every website or web application needs to send transactional emails at one time or another. A user may request a password reset that sends them an email, or you may need to notify a user through email. So it is necessary to use the right tool when you don’t want to handle everything by yourself.

So Let’s discuss what are the options we have got here.

Whatever language you are working with you do need a mail server. So you can
1.Host your own mail server
It is as complicated as it sounds. There are so many components to handle like

  • Mail transfer agent
  • DNS records
  • Security certificates

Apart from this, maintenance is time consuming. You have to put efforts in

  • Staying off the black lists (not to be listed as spammer)
  • Troubleshooting
  1. Buy space on a mail server provided by vendors like zoho, fastmail, Gandi, Microsoft office 365 etc. It comes with lots of benefits like
  • They specialize in sending and logging mails.
  • They will increase your deliverability rate using high end servers and technologies.
  • Your mail will avoid being labeled “spam” and/or blocked completely (quite common)
  • You will avoid headaches in setting up your in-house mail server.

3. Use SMTP to send mail through personalized mailbox provided by Gmail,Yahoo,Outlook etc.

Let’s discuss about the three major players Gmail,Yahoo and Outlook if you have made your mind to use their mail server through SMTP for minimum overhead.

Since you are going to send emails on behalf of your clients/users via your application through their personal email id, you have to store the credentials of the users. And every time while sending mails you have to use it to connect to SMTP servers. Mails will be sent as they were composed personally as the FROM header will have Name and Email Id of the user.

Now you just have to find the right way to connect to their smtp server.

So if you are using PHP you would be using PHPMailer or Swiftmailer etc. Java guys have JavaMail Api and every other language have their own mailer client or there are third parties for that.

All you have to do is to pass your email Id or user name with few more settings variables.

Here are the list of setting variables that you need to know.

  1. Gmail

Address :
Port : 465
Security/Connection Type: ssl
AuthMode : login

  1. Yahoo

Address :
Port : 465
Security/Connection Type: ssl
AuthMode : login

  1. Live/Hotmail/Outlook

Address :
Port : 587
Security/Connection Type: tls
AuthMode : login

Now the probable errors that you may face while connecting and sending.

1.Limit for sending email is exceeded

  • Every email provider has limitation on the daily limit of the mails that can be send.

2.Authentication Failed.

  •  It says that either your Email Id/Username or password is wrong.

3.From address is not verified

  • It is specific for Yahoo.
  • Every public mail server allow only emails with actual author’s email in “From” header. But except yahoo other silently messes with the header but yahoo throws error.

Ex- If your email id is and while setting the “From” field is set to

4.Connection could not be established with the server.

  • It rarely happens but happens when the server refuses the connection because of the bulk hit while sending emails.

5.Illegal Access Error

  • The better the service provider the better it will guard you against security breaches. The moment you try to establish connection to the SMTP server it will refuse the connection because you have to tell the email providers to allow you to connect.
  • Gmail tells you to choose the option of allowing less secure apps to establish connection from their sign-in and security setting. Yahoo/Outlook will tell you to do the same from their account security setting.
  • But it may be possible that after all of this they even send you an email stating your access time/location and requesting you to verify that, so do check your inbox.

There are other errors too but are straightforward to read and solve.

Now may be you are expecting that you will find the email in the sent item folder.

Gmail/Outlook will fulfill your requirement but yahoo doesn’t bother to save the sent emails. But if you want to do this just fetch yahoo IMAP setting, establish one more connection after sending mail and write the email to the sent item folder by yourself. Php and Java both provide append functions to do so.(Refer the Example)

Example – (Using PHPMailer and Gmail)

$mail = new PHPMailer();
$body = file_get_contents('body.html');
$body = eregi_replace("[\]",'',$body);
$mail->SMTPAuth = true;
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Host = "";
$mail->Port = 465;
$mail->Username = "";
$mail->AddReplyTo("","Sender Name");
$mail->Subject = "This is a Test Mail.";
$address = "";
$mail->AddAddress($address, "Recipient Name");
$mail->AddAttachment("image.png"); // attachment
$mail->AddAttachment("document.pdf"); // attachment

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
//Code to save Sent Mail in the Sent Folder
$message = $mail->MIMEHeader . $mail->MIMEBody;
$path = "INBOX" . (isset($folderPath) && !is_null($folderPath) ? ".".$folderPath : ""); // Location to save the email
$imapStream = imap_open("{" . $mail->Host . "}" . $path , $mail->Username, $mail->Password);
imap_append($imapStream, "{" . $mail->Host . "}" . $path, $message);

*Above Code is just a reference code not written for copy and run.

Apart from it’s ease of use there are few more benefits.
1. Personalised feel:
Emails will be sent with personal email id.
2.  Anti-Spam benefits:
Emails are under all kinds of anti-spam scrutiny by major email providers.

1. Sending Mails in Bulk:
All the providers have limitation on number of emails sent hourly or daily.
Sending large amount of mails in short span of time will also result in getting blocked.
2. Credentials Change:
In case any User has changed their password there is no way of getting it known
beforehand until you develop a logic for that in your application.
3. Debugging:
In case of failure you can’t trace it because most of the things are wrapped in a
single and broad error message.
4. Security Risk:
Since you ask users for the credentials and store it for the SMTP connection, you
should ensure it’s safety. (Always save the credentials encrypted.)

You’re now ready to start sending mails using the power of the smtp servers which you don’t have to maintain, but with a little restrictions mentioned above. Though it is quite simple but you must explore the services where you have more power to customize and configure at your own will.

Posted in Web Technology