Sending Emails Via Gmail SMTP With Perl

On December 8, 2010, in PERL, by Dipin Krishna

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;
Tagged with:  

16 Responses to “Sending Emails Via Gmail SMTP With Perl”

  1. Mark says:

    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.

    • Mark says:

      To send to multiple recipients, you issue multiple…

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

      • You can put the recipients into and array:

        my @recipients = ('recipient1@dipinkrishna.com', 'recipient2@dipinkrishna.com');
        $smtp->recipient(@recipients);
  2. Lavanya says:

    HI,

    It worked for me.
    Thanks a lot.

    Can you publish how to add attachments also?

  3. Hi Priya,
    This means that you are not able to connect directly to the gmail server. Plz try after disabling your filrewall.

  4. priya says:

    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. Lakshmi says:

    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. phani says:

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

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

  7. steve says:

    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. Piter says:

    $smtp->datasend(“\nThis is a test mail body”); – I mean here *****
    piter

  9. Piter says:

    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

  10. […] Click here for the sample script If you enjoyed this article, please consider sharing it!   […]

Leave a Reply

*

Search