Sending Email with Perl and Python

By Xah Lee. Date:


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

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.sendmail(fromAddr, toAddr, text)

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.

20.12. smtplib — SMTP protocol client — Python v2.7.6 documentation #smtp-example


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;

perldoc perlfaq9

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

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 “Two modules and a few links related to Perl and Internet mail” (1999) by Milivoj Ivkovic.

If you have a question, put $5 at patreon and message me.