Sending Email with Perl and Python

Xah Lee, 2005-01-29

Python

Suppose you want to spam your friend, and you have lots of friends. The solution is to write a program to do it. Here's how you'd do it:

# -*- coding: utf-8 -*-
# Python

import smtplib

smtpServer='smtp.yourdomain.com';
fromAddr='xah@xahlee.org';
toAddr='xah@xahlee.org';
text='''Subject: newfound love

Hi friend,

long time no write, i have a new manifesto i
 think it would be of interest for you to peruse.
...
'''

server = smtplib.SMTP(smtpServer)
server.set_debuglevel(1)
server.sendmail(fromAddr, toAddr, text)
server.quit()

Note: the set_debuglevel() is nice because you see all the interactions with the smtp server. Useful when you want to see what's going on with a smtp server.

Reference: Python Doc↗.

Perl

There are several perl packages for sending email. Here's a example of sending email using the package Mail::Mailer. This example is from perlfaq9.

# perl
use Mail::Mailer;

$mailer = Mail::Mailer->new();
$mailer->open({ From    => $from_address,
                To      => $to_address,
                Subject => $subject,
              })
  or die "Can't open: $!\n";
print $mailer $body;
$mailer->close();

Reference: perldoc perlfaq9.html↗.

In Perl, there are several packages that do send mail, each with slight problems. Here's how the situation are back in 2001 March.

For Perl libraries that deals with RFC 821:

The first two has bugs. I forgot what they are exactly. I think Mail::Mailer has a bug on the from field. i.e. it ignores what you gave. I'm currently using Mail::Sendmail, and according to a ex-colleague, it has problems with some DNS mail exchange entries.

For some discussion of Perl mail modules and their shortcomings, see http://alma.ch/perl/mail.htm.


Page created: 2005-01.
© 2005 by Xah Lee.
Xah Signet