Problem
How do I set up outbound SMTP using Perl?
How do I use outMail in Perl?
How do I set up outbound SMTP using Mail::Sendmail?
Solution
The following example of code shows you how to send an email using outMail as the SMTP SmartHost mail relay using the Mail::Sendmail module for perl.
outMail is an authenticated SMTP relay so the example below shows authentication in perl as well. The code below uses the default SMTP port 25 but it can easily be changed to an alternative SMTP port by editing the line code $mail{port}.
Example code smtp-test.pl
#!/usr/bin/perl -w
use Mail::Sendmail;
use strict;
use warnings;
# #################################################################
# define the e-mail participants, server and content
# #################################################################
# create the main e-mail hash structure [REQUIRED]
my %mail;
# set the smtp mail server [REQUIRED]
$mail{Smtp} = "mxXXXXXX.smtp-engine.com";
$mail{Debug} = 6;
$mail{Port} = 25;
$mail{Auth} = {user => "outmail-username", pass => "outmail-password",
method => "LOGIN", required => 1};
# set the recipients (to) address [REQUIRED]
$mail{To} = 'recipient@example.com';
# set the mail sender address [REQUIRED]
$mail{From} = 'me@example.com';
$mail{Sender} = 'me@example.com';
# set the mail subject line
$mail{subject} = "Test message";
# set the mail content
$mail{body} = "The test messsage is having this body line inside.";
# set the mail encoding type
$mail{'content-type'} = qq(text/plain; charset="utf-8");
# #################################################################
# send the e-mail out and return verbose information
# #################################################################
sendmail(%mail) or die $Mail::Sendmail::error;
print "The sendmail log reports:\n".$Mail::Sendmail::log."\n";
exit 0;
Summary of server details
Outgoing server |
mxXXXXXX.smtp-engine.com As provided in your signup email. |
Outgoing server protocol |
SMTP |
Outgoing server port |
25, 465, 587, 2525 or 8025 |
Authentication Type |
Basic Authentication, SSL and TLS supported |
Username |
As provided |
Password |
As provided |