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 => '[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]');
$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;
You can put the recipients into and array:
To send to multiple recipients, you issue multiple…
$smtp->recipient(‘[email protected]’)
$smtp->recipient(”[email protected]’);
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.
See https://dipinkrishna.com/blog/2012/12/perl-send-email-attachment-gmail-smtp/
HI,
It worked for me.
Thanks a lot.
Can you publish how to add attachments also?
Hi Priya,
This means that you are not able to connect directly to the gmail server. Plz try after disabling your filrewall.
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”
I don’t think Net::SMTP has anything like that.
You can use HTML::Template for creating the message, and put its output to datasend() of Net::SMTP (Net::CMD).
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
Could you try telnet smtp.gmail.com 587 from your host.
hi i am getting connection time out, even if i given time out value as 3000.
Connect failed :IO::Socket::INET: connect: timeout
Hi Steve, See http://sourceforge.net/mailarchive/message.php?msg_id=29238006
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?
$smtp->datasend(“\nThis is a test mail body”); – I mean here *****
piter
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
Pingback: Sending Emails Via Gmail SMTP With Perl | Linux Tips