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 ‘smtp.gmail.com’ and the port is 587.

#!/usr/bin/perl
#-------------------------------------------------#
#  File:    sendEmail.pl                          #
#  Author:  Dipin Krishna                         #
#-------------------------------------------------#

use Net::SMTP::TLS;

my $smtp = new Net::SMTP::TLS(
	'smtp.gmail.com',
	Port    =>	587,
	User    =>	'sender@test.com',
	Password=>	'Password',
	Timeout =>	30
	);

#  -- Enter email FROM below.   --
$smtp->mail('sender@test.com');

#  -- Enter recipient mails addresses below --
my @recipients = ('recipient1@test.com', 'recipient2@test.com');
$smtp->recipient(@recipients);

$smtp->data();

#This part creates the SMTP headers you see
$smtp->datasend("To: recipient1\@test.com\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("\n");
$smtp->datasend("This is a test mail body");
$smtp->datasend("\n");
$smtp->dataend();

$smtp->quit;

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

  1. You can put the recipients into and array:

    my @recipients = ('recipient1@dipinkrishna.com', 'recipient2@dipinkrishna.com');
    $smtp->recipient(@recipients);
  2. To send to multiple recipients, you issue multiple…

    $smtp->recipient(‘recipient1@dipinkrishna.com’)
    $smtp->recipient(”recipient2@dipinkrishna.com’);

  3. 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.

  4. 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 smtp.gmail.com 587 it returned “Connecting To smtp.gmail.com…Could not open connection to the host, on port 58
    7: Connect failed”

  5. 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.

    TIA,
    Lakshmi

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

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

  7. 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/SSL.pm line 332

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

  8. 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.
    Thanks
    Piter

  9. 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.