Site icon Dipin Krishna

Sending Emails Via Gmail SMTP With Perl

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;
Exit mobile version