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?
问题:
回答1:
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 $@;
回答2:
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.
回答3:
I've always used and had very good luck with Mail::Sender.
回答4:
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)
回答5:
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?
回答6:
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 });
回答7:
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
回答8:
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?
回答9:
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