12

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?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
git-noob
  • 5,757
  • 11
  • 34
  • 32
  • I don't really get the question: Do you want to talk directly to Google's SMTP server? Or your own? Is there anything gmail-specific about this question? – innaM Feb 18 '09 at 10:19

9 Answers9

12

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 => 'username@gmail.com',
        Password => 'password',
        Hello => 'fayland.org',
    ]
} );

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

eval { $mailer->send($email) };
die "Error sending email: $@" if $@;
jfs
  • 399,953
  • 195
  • 994
  • 1,670
Fayland Lam
  • 1,016
  • 8
  • 11
6

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.

xdg
  • 2,975
  • 19
  • 17
2

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

Joe Casadonte
  • 15,888
  • 11
  • 45
  • 57
1

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      => 'mymail@gmail.com',
    From    => 'sender@example.com',
    Subject => 'Hi!',
  ],
  body => "This is my message\n",
);

sendmail($email, { transport => $transport });
JosephH
  • 37,173
  • 19
  • 130
  • 154
1

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?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Simon
  • 12,018
  • 4
  • 34
  • 39
1

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)

Mark Fowler
  • 1,264
  • 11
  • 20
0

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

Kaypro II
  • 3,210
  • 8
  • 30
  • 41
0

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

Schildmeijer
  • 20,702
  • 12
  • 62
  • 79
0

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?

Community
  • 1
  • 1
spoulson
  • 21,335
  • 15
  • 77
  • 102