Using Perl to send emails from remote server

On DigitalOcean I’ve got a vps running MIAB at box.ezemailserver.com and a “Web App” server on another vps at cherrypc.com.

I want to send emails from the web app using a Perl script.

I’m using the MIAB DNS server for both domain names. With the MIAB server I can send and receive email using my Mac Mini “Mail” app so that’s working great.

I’ve edited a perl script that attempts to authenticate a user and send a simple email from cherrypc.com but it is not passing the auth stage. I get this error:

Error sending email: failed AUTH: Command unknown: 'AUTH

Anyone have any ideas on what I’m doing wrong?

Here’s the script on the web app server, the mail.log from the MIAB, what the script outputs when run:


test3.pl

#!/usr/bin/perl

use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTPS;
use Try::Tiny;

my $transport = Email::Sender::Transport::SMTPS->new(
		host => 'box.ezemailserver.com',
		ssl  => 'starttls',
		sasl_username => 'hostmaster@ezemailserver.com',
		sasl_password => 'xxxx',
		debug => 1, # or 1
);

use Email::Simple::Creator; # or other Email::
my $message = Email::Simple->create(
		header => [
				From    => 'hostmaster@ezemailserver.com',
				To      => 'bill@secureshopper.com',
				Subject => 'cherrypc perl test',
		],
		body => 'This is the test.',
);

try {
		sendmail($message, { transport => $transport });
} catch {
		die "Error sending email: $_";
};

MIAB mail.log:

Apr 16 13:17:10 box postfix/submission/smtpd[31729]: connect from cherrypc.com[45.55.229.99]
Apr 16 13:17:10 box postfix/submission/smtpd[31729]: lost connection after EHLO from cherrypc.com[45.55.229.99]
Apr 16 13:17:10 box postfix/submission/smtpd[31729]: disconnect from cherrypc.com[45.55.229.99]


cherrypc.com perl script output

root@cherrypc:/usr/lib/cgi-bin# perl test6.pl
Net::SMTPS>>> Net::SMTPS(0.06)
Net::SMTPS>>> IO::Socket::IP(0.37)
Net::SMTPS>>> IO::Socket(1.38)
Net::SMTPS>>> IO::Handle(1.35)
Net::SMTPS>>> Exporter(5.72)
Net::SMTPS>>> Net::SMTP(3.10)
Net::SMTPS>>> Net::Cmd(3.10)
Net::SMTPS=GLOB(0x33b7958)<<< 220 box.ezemailserver.com ESMTP Hi, I’m a Mail-in-a-Box (Ubuntu/Postfix; see https://mailinabox.email/)
Net::SMTPS=GLOB(0x33b7958)>>> EHLO localhost.localdomain
Net::SMTPS=GLOB(0x33b7958)<<< 250-box.ezemailserver.com
Net::SMTPS=GLOB(0x33b7958)<<< 250-PIPELINING
Net::SMTPS=GLOB(0x33b7958)<<< 250-SIZE 134217728
Net::SMTPS=GLOB(0x33b7958)<<< 250-VRFY
Net::SMTPS=GLOB(0x33b7958)<<< 250-ETRN
Net::SMTPS=GLOB(0x33b7958)<<< 250-STARTTLS
Net::SMTPS=GLOB(0x33b7958)<<< 250-ENHANCEDSTATUSCODES
Net::SMTPS=GLOB(0x33b7958)<<< 250-8BITMIME
Net::SMTPS=GLOB(0x33b7958)<<< 250 DSN
Error sending email: failed AUTH: Command unknown: ‘AUTH’

Trace begun at /usr/local/share/perl/5.22.1/Email/Sender/Transport/SMTPS.pm line 98
Email::Sender::Transport::SMTPS::_throw(‘Email::Sender::Transport::SMTPS=HASH(0x24d8b80)’, ‘failed AUTH’, ‘Net::SMTPS=GLOB(0x33b7958)’) called at /usr/local/share/perl/5.22.1/Email/Sender/Transport/SMTPS.pm line 71
Email::Sender::Transport::SMTPS::_smtp_client(‘Email::Sender::Transport::SMTPS=HASH(0x24d8b80)’) called at /usr/local/share/perl/5.22.1/Email/Sender/Transport/SMTPS.pm line 107
Email::Sender::Transport::SMTPS::send_email(‘Email::Sender::Transport::SMTPS=HASH(0x24d8b80)’, ‘Email::Abstract=ARRAY(0x3023ae0)’, ‘HASH(0x3023ca8)’) called at /usr/local/share/perl/5.22.1/Email/Sender/Role/CommonSending.pm line 45
Email::Sender::Role::CommonSending::try {…} at /usr/share/perl5/Try/Tiny.pm line 92
eval {…} at /usr/share/perl5/Try/Tiny.pm line 83
Try::Tiny::try(‘CODE(0x307b370)’, ‘Try::Tiny::Catch=REF(0x3023d38)’) called at /usr/local/share/perl/5.22.1/Email/Sender/Role/CommonSending.pm line 58
Email::Sender::Role::CommonSending::send(‘Email::Sender::Transport::SMTPS=HASH(0x24d8b80)’, ‘Email::Abstract=ARRAY(0x3023ae0)’, ‘HASH(0x3069a30)’) called at /usr/local/share/perl/5.22.1/Email/Sender/Simple.pm line 119
Email::Sender::Simple::send_email(‘Email::Sender::Simple’, ‘Email::Abstract=ARRAY(0x3023ae0)’, ‘HASH(0x3023cf0)’) called at /usr/local/share/perl/5.22.1/Email/Sender/Role/CommonSending.pm line 45
Email::Sender::Role::CommonSending::try {…} at /usr/share/perl5/Try/Tiny.pm line 92
eval {…} at /usr/share/perl5/Try/Tiny.pm line 83
Try::Tiny::try(‘CODE(0x3023b58)’, ‘Try::Tiny::Catch=REF(0x3005228)’) called at /usr/local/share/perl/5.22.1/Email/Sender/Role/CommonSending.pm line 58
Email::Sender::Role::CommonSending::send(‘Email::Sender::Simple’, ‘Email::Simple=HASH(0x3004f88)’, ‘HASH(0x3004ee0)’) called at /usr/share/perl5/Sub/Exporter/Util.pm line 18
Sub::Exporter::Util::ANON(‘Email::Simple=HASH(0x3004f88)’, ‘HASH(0x3004ee0)’) called at test6.pl line 26
main::try {…} at /usr/share/perl5/Try/Tiny.pm line 92
eval {…} at /usr/share/perl5/Try/Tiny.pm line 83
Try::Tiny::try(‘CODE(0x2fd50b0)’, ‘Try::Tiny::Catch=REF(0x2f96d08)’) called at test6.pl line 29

Ok, I learned a lot working on this and I still don’t know way more than I probably should but here’s what I did and a working perl script for those who might want or need one.

The script is running on a DigitalOcean vps with Ubuntu 16.04 and sending the email from another DigitalOcean vps running MIAB. You may have to install the perl modules referenced and first I needed to add my remote domain’s IP address to POSTFIX on the MIAB server like so:

Edit this file:
sudo nano /etc/postfix/main.cf

Look for this line:
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128

Add the remote server IP like this (example IP used: 199.99.99.99):
mynetworks = 199.99.99.99 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128

Save the file then reload postfix:
sudo postfix reload

Here’s the perl script. I put mine in the “/usr/lib/cgi-bin/” directory:

#!/usr/bin/perl

use Net::SMTP;
use MIME::Base64;

# Server and User Authentication:
my $smtpserver = 'box.youremailserver.com';
my $user = 'you@yourwebsite.com';
my $password = 'your-password';

my $from ='you@yourwebsite.com';

# Test email data:
my $send_to = 'test@somewhere.com';
my $send_subj = 'Perl on MAIB Test';
my $html_msg = '<h3>This is the HTML test message</h3>';

&doMail ($send_to, $send_subj, $html_msg);

sub doMail {

	my ($to, $subj, $msg) = @_;	
	
	my $smtp = Net::SMTP->new(
		$smtpserver,
		   Timeout => 30,
		   Debug   => 0,
	);

	$smtp->datasend("AUTH LOGIN\n");
	$smtp->response();

#  -- Login to SMTP --
	# Enter sending email box address username below.
	$smtp->datasend(encode_base64($user) );
	$smtp->response();

	#  -- Enter email box address password below.   login to SMTP --
	$smtp->datasend(encode_base64($password) );
	$smtp->response();
			  
	$smtp->mail($from);

	$smtp->to($to);

	$smtp->data();

#  -- This part creates the SMTP headers you see --
	$smtp->datasend("From: $from \n");
	$smtp->datasend("To: $to \n");
	$smtp->datasend("Content-Type: text/html \n");
	$smtp->datasend("Subject: $subj");

	# then a line break to separate headers from message body
	$smtp->datasend("\n");

	# the html message:
	$smtp->datasend($msg);

	$smtp->datasend("\n");

	$smtp->dataend();
	
	if ($smtp->error) {print $smtp->error}

	else {print "Message Sent \n"}

	$smtp->quit;

	return;
}