in reply to Re: Send email via Gmail
in thread Send email via Gmail

Ah ok, making some progress here :) The below works:

use Net::SMTP; my $smtp = Net::SMTP->new('smtp.gmail.com', Hello => 'steampunkjunkies.net', Timeout => 30, Debug => 1, SSL => 1 ) || die "Error: $!"; $smtp->auth($CFG->{db_smtp_user}, $CFG->{db_smtp_pass}) or die "C +ould not authenticate with mail.\n"; $smtp->mail('andy@steampunkjunkies.net'); # from addr $smtp->to('foo@gmail.com'); $smtp->data(); $smtp->datasend("foo\n"); $smtp->quit();


The problem with that though, is that I actually want to send a fully compiled email already (built up using MIME::Lite). So something like:

$smtp->data(); $smtp->datasend($body_of_a_full_email_from_MIME_Lite); $smtp->quit();


Do you understand what I mean?

Thanks!

Replies are listed 'Best First'.
Re^3: Send email via Gmail
by stevieb (Canon) on Oct 08, 2016 at 15:18 UTC

    What happens when you try it? What error(s) do you get?

      Aahhh I got it! When building up the original email, I was doing:
      my $msg = MIME::Lite->new( From => $from, To => $to, Type => 'multipart/alternative', Subject => $subject_val, );


      But obviously Net::SMTP does the to/from part itself. Removing it so I just had:

      my $msg = MIME::Lite->new( Type => 'multipart/alternative', Subject => $subject_val, );


      ...and it works like a charm now. Thanks goodness for that!

      Now I can relax a bit for the rest of the weekend haha

      Thanks to both of you :) So for anyone who may come across this post in the future, here is the code I ended up using (appologies for not making it super pretty, but I need to call it a day :))
      my $msg = MIME::Lite->new( Type => 'multipart/alternative', Subject => $subject_val, ); my $att_text = MIME::Lite->new( Type => 'text', Data => "plain text version", Encoding => 'quoted-printable', ); $att_text->attr('content-type' => 'text/plain; charset=UTF-8'); $msg->attach($att_text); my $att_html = MIME::Lite->new( Type => 'text', Data => "<b>html version</b> goo", Encoding => 'quoted-printable', ); $att_html->attr('content-type' => 'text/html; charset=UTF-8'); $msg->attach($att_html); my $email = $msg->as_string(); use Net::SMTP; my $smtp = Net::SMTP->new('smtp.gmail.com', Hello => 'domain.net', Timeout => 30, Debug => 1, SSL => 1 ) || die "Error: $!"; $smtp->auth($CFG->{db_smtp_user}, $CFG->{db_smtp_pass}) or die "Co +uld not authenticate with mail.\n"; $smtp->mail('you@gmail.com'); # from addr $smtp->to('foo@bar.com'); $smtp->data(); $smtp->datasend($email); $smtp->quit();