Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send email to my Gmail account using SMTP and Perl?

I don't want to use sendmail to send an email but would prefer to use SMTP. How can I use Perl to send an email to my GMAIL account?

like image 637
git-noob Avatar asked Feb 18 '09 10:02

git-noob


People also ask

How can you send email using Perl script?

Sending a Plain Message If you are working on Linux/Unix machine then you can simply use sendmail utility inside your Perl program to send email. Here is a sample script that can send an email to a given email ID. Just make sure the given path for sendmail utility is correct.

What is the outgoing SMTP for Gmail?

The outgoing SMTP server, smtp.gmail.com , requires TLS. Use port 465 , or port 587 if your client begins with plain text before issuing the STARTTLS command.


6 Answers

personally I would suggest you to use my module Email::Send::SMTP::TLS which works pretty well through the TLS of Google Mail.

Thanks.

use Email::Send;

my $mailer = Email::Send->new( {
    mailer => 'SMTP::TLS',
    mailer_args => [
        Host => 'smtp.gmail.com',
        Port => 587,
        User => '[email protected]',
        Password => 'password',
        Hello => 'fayland.org',
    ]
} );

use Email::Simple::Creator; # or other Email::
my $email = Email::Simple->create(
    header => [
        From    => '[email protected]',
        To      => '[email protected]',
        Subject => 'Subject title',
    ],
    body => 'Content.',
);

eval { $mailer->send($email) };
die "Error sending email: $@" if $@;
like image 152
Fayland Lam Avatar answered Oct 03 '22 15:10

Fayland Lam


As per the comment, it's not clear if you want to send email via Google's SMTP, or just send email in general (perhaps to your gmail account). You should check out Email::Send and possibly Email::Send::Gmail.

Alternatively, if what you're really asking is how do I move email from somewhere that isn't Gmail to Gmail, I've had very good luck with IMAP using Mail::Box and the Mail::Box::IMAP4::SSL backend. You can see an example of use here.

like image 37
xdg Avatar answered Oct 03 '22 16:10

xdg


I've always used and had very good luck with Mail::Sender.

like image 37
Joe Casadonte Avatar answered Oct 03 '22 15:10

Joe Casadonte


Another possibility you might want to look at is using the Email::Send::Gmail module from CPAN. This will allow you to send email from your Gmail account to any account (for example, to yourself)

like image 28
Mark Fowler Avatar answered Oct 03 '22 16:10

Mark Fowler


There are muliple SMTP modules on CPAN, for example Net::ESMTP. Also, sendmail very probably does use SMTP to communicate with mail servers, so what's your real reason for not wanting to use it?

like image 30
Simon Avatar answered Oct 03 '22 16:10

Simon


Email::Send (as used in Fayland Lam's answer) is deprecated:

Email::Send is going away... well, not really going away, but it's being officially marked "out of favor."

This works for me, using the preferred Email::Sender:

use strict;
use warnings;

use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTPS ();
use Email::Simple ();
use Email::Simple::Creator ();

my $smtpserver = 'server';
my $smtpport = 587;
my $smtpuser   = 'username';
my $smtppassword = 'password';

my $transport = Email::Sender::Transport::SMTPS->new({
  host => $smtpserver,
  port => $smtpport,
  ssl => "starttls",
  sasl_username => $smtpuser,
  sasl_password => $smtppassword,
});

my $email = Email::Simple->create(
  header => [
    To      => '[email protected]',
    From    => '[email protected]',
    Subject => 'Hi!',
  ],
  body => "This is my message\n",
);

sendmail($email, { transport => $transport });
like image 30
JosephH Avatar answered Oct 03 '22 16:10

JosephH