Sending mail via SMTP in Perl

ou have to start a SMTP session (after authorization, if necessary) with a MAIL command giving the sender's email address. That's why the responses say "MAIL first" (5xx is an error response).

Sending mail via SMTP in Perl
You have to start a SMTP session (after authorization, if necessary) with a MAIL command giving the sender's email address. That's why the responses say "MAIL first" (5xx is an error response). So:
$smtp->auth($smtpuser, $smtppassword);
$smtp->mail('sender@example.com');
$smtp->to('mymail@gmail.com');

But if you're not a SMTP expert, why not use a higher-level module like Email::Sender instead of the low-level Net::SMTP?

use strict;
use warnings;

use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP ();
use Email::Simple ();
use Email::Simple::Creator ();

my $smtpserver = 'server';
my $smtpport = 25;
my $smtpuser   = 'username';
my $smtppassword = 'password';

my $transport = Email::Sender::Transport::SMTP->new({
  host => $smtpserver,
  port => $smtpport,
  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 });