Sending Email with Perl & Python

,

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.example.com';
fromAddr='boss@example.com';
toAddr='jane@example.com';
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.

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

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();

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. http://alma.ch/perl/mail.htm

blog comments powered by Disqus