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?
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?
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
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.
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?
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 $@;
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)
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%5Ftutorial.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
There is my example of the perl script . It send's and get emails to\from your gmail account: perl script