How do I send email to my Gmail account using SMTP

2019-03-10 02:09发布

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?

9条回答
Deceive 欺骗
2楼-- · 2019-03-10 02:33

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)

查看更多
一纸荒年 Trace。
3楼-- · 2019-03-10 02:37

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 });
查看更多
祖国的老花朵
4楼-- · 2019-03-10 02:37

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

查看更多
地球回转人心会变
5楼-- · 2019-03-10 02:38

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 $@;
查看更多
放我归山
6楼-- · 2019-03-10 02:39

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

查看更多
成全新的幸福
7楼-- · 2019-03-10 02:43

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?

查看更多
登录 后发表回答