Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

gmail and Net::SMTP

by morgon (Priest)
on Mar 12, 2020 at 18:42 UTC ( #11114183=perlquestion: print w/replies, xml ) Need Help??

morgon has asked for the wisdom of the Perl Monks concerning the following question:


I cannot seem to figure out how to send mail via gmail using Net::SMTP (that's what you're supposed to use right?).

Here is what I try:

use strict; use Net::SMTP; my $from = ''; my $full_name = "Some Body"; my $pw = "my_password"; my $to = ''; my $smtp = Net::SMTP->new('', Port=> 587, User => $from, Password => $pw ); $smtp->mail($from); $smtp->recipient($to); $smtp->data(); $smtp->datasend("To: $to"); $smtp->datasend("From: $full_name\n"); $smtp->datasend("Content-Type: text/plain \n"); $smtp->datasend("Subject: A Test Mail"); $smtp->datasend("\n"); $smtp->datasend("This is a test mail body"); $smtp->datasend("\n"); $smtp->dataend(); $smtp->quit();
It all looks very silly to me and (even worse) it does not work.

I do not get an error, but no mail seems to get sent.

So my question: How do you send mail via gmail using perl in the year 2020?

Many thanks!


Why do code-blocks look so bad now?

EDIT2: Because copying the code introduced a lot of white space - fixed that.

Replies are listed 'Best First'.
Re: gmail and Net::SMTP
by pryrt (Abbot) on Mar 12, 2020 at 19:18 UTC
    Did you read gmail sending in 2020, which seems to be on your exact topic? It looks like Email::Sender was the recommended sending module.

    Regarding code blocks: remove the huge number of trailing spaces from your code, and it would look fine.

      Evidently I have not read that.

      Every time I try to send email in a script (every 5 years or so) things have changed beyond recognition.

      Oh well...

        Perhaps you mean, "Every time I try to send email via gmail in a script (every 5 years or so) Google has changed things beyond recognition"? Personally, I've been using one of Mail::Sendmail or Net::SMTPS/Net::SMTP for the past couple of decades without significant problems. But that's probably because I'm not using Google to perform the sending. YMMV.

Re: gmail and Net::SMTP
by bliako (Monsignor) on Mar 12, 2020 at 21:18 UTC

    I have successfully used code like below for sending via my local net-provider:

    $response = Email::Sender::Simple::sendmail( $emailobj, {'transport' => Email::Sender::Transport::SMTPS->new($smtp_params)} );

    where $emailobj is produced using Email::MIME->create() and then keep adding attachments to it.

    There are a few examples at the manpage of Email::MIME. If you need more concrete help let us know. I am not posting anything else as I don't know if this is a 2020-approved method.

    bw, bliako

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11114183]
Approved by haukex
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2023-03-27 10:21 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (64 votes). Check out past polls.