Skip to content
Blog Sending Emails Via Gmail SMTP With Perl

Sending Emails Via Gmail SMTP With Perl

Update: Click here If you want to Send an Email with attachment through Gmail SMTP.

This is a tutorial on how to send mails using Perl Net::SMTP module through Gmail SMTP.

The smtp module implements a client interface to the SMTP and ESMTP protocol, enabling a perl5 application to talk to SMTP servers.

The first step is to create a SMTP connection to the server. A new Net::SMTP object must be created with the new method. Once this has been done, all SMTP commands are accessed through this object. The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used).

Remember Google’s SMTP server is ‘’ and the port is 587.

#  File:                          #
#  Author:  Dipin Krishna                         #

use Net::SMTP::TLS;

my $smtp = new Net::SMTP::TLS(
	Port    =>	587,
	User    =>	'[email protected]',
	Password=>	'Password',
	Timeout =>	30

#  -- Enter email FROM below.   --
$smtp->mail('[email protected]');

#  -- Enter recipient mails addresses below --
my @recipients = ('[email protected]', '[email protected]');


#This part creates the SMTP headers you see
$smtp->datasend("To: recipient1\\n");
$smtp->datasend("From: Test Name \n");
$smtp->datasend("Content-Type: text/html \n");
$smtp->datasend("Subject: A Test Mail");
# line break to separate headers from message body
$smtp->datasend("This is a test mail body");


16 thoughts on “Sending Emails Via Gmail SMTP With Perl”

  1. Tags didn’t post correctly… instead of surrounding recipient addresses in quotes, use less than and greater than delimiters…

    That corrected delivery for me…

    Thanks for the example.

  2. hi,
    i am getting connection time out, even if i given time out value as 3000.

    Connect failed :IO::Socket::INET: connect: timeout

    I tried telnet 587 it returned “Connecting To…Could not open connection to the host, on port 58
    7: Connect failed”

  3. Hi,

    is there any option available to fetch the email message content from a seperate file more like a template using Net::SMTP.

    It is available for Mime::Lite as far as i know..but due to certain restrictions..i am forced to use Net::SMTP.


  4. hi i am getting connection time out, even if i given time out value as 3000.

    Connect failed :IO::Socket::INET: connect: timeout

  5. I really appreciate this tutorial. unfortuately i’m getting this error:

    invalid SSL_version specified at /usr/lib/perl5/site_perl/5.8.8/IO/Socket/ line 332

    do you have any ideas as to what is causing it?

  6. Very helpfull, had a problen with body not displaying on yahoo but added ‘\n’ here:
    $smtp->datasend(“This is a test mail body”);

    and not sure why but it helped.

  7. Pingback: Sending Emails Via Gmail SMTP With Perl | Linux Tips

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.