git-noob
git-noob

Reputation: 5937

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?

Upvotes: 12

Views: 25783

Answers (9)

JosephH
JosephH

Reputation: 37495

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 });

Upvotes: 1

Kaypro II
Kaypro II

Reputation: 3340

If you just don't like sendmail, another option is to use Postfix, another MTA.

Here are the instructions I followed to get it setup on my machine, using gmail: http://souptonuts.sourceforge.net/postfix_tutorial.html

This might be useful too, if you get a warning about failing to verify a certificate from Thawte Premium Server CA. http://ubuntuforums.org/archive/index.php/t-894355.html

Upvotes: 0

Simon
Simon

Reputation: 12478

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?

Upvotes: 1

Mark Fowler
Mark Fowler

Reputation: 1264

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)

Upvotes: 1

Fayland Lam
Fayland Lam

Reputation: 1016

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 $@;

Upvotes: 12

Joe Casadonte
Joe Casadonte

Reputation: 16859

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

Upvotes: 2

spoulson
spoulson

Reputation: 21591

I happen to use MIME::Lite, which is a wrapper around Net::SMTP to simplify the process of building email objects, file attachments, and sending the payload.

If you're not familiar with installing modules, check:

On Windows, use the ActiveState Perl Package Manager (in start menu)

On Unix, use CPAN: $ sudo cpan Module::Name

On hosted Unix accounts: How can I install a CPAN module into a local directory?

Upvotes: 0

xdg
xdg

Reputation: 2995

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.

Upvotes: 6

Schildmeijer
Schildmeijer

Reputation: 20946

If you aren't familiar with CPAN (Comprehensive Perl Archive Network) I recommend you to bookmark that site. It contains third party (mostly well tested) libraries.

An example showing how to send emails using perl: http://www.perlfect.com/articles/sendmail.shtml

Upvotes: 0

Related Questions