Camelia

Sending email using Perl 6

I had a little Déjà vu feeling when I wrote this code. In a way it looks very much like the solutions we used to have in Perl 5, before the explosion of e-mail sending modules.

It is far from perfect, but it works. I use it to send out the Perl 6 newsletter.

Note! This site is about Perl 6, the future version of Perl.
If you are looking for a solution for the current production version of Perl 5, please check out the Perl 5 tutorial.

The code

use v6;

sub sendmail(*%mail) {
  die "Missing $_" if not %mail{$_} for <To From Subject Text> ;

  my $file = '/tmp/perl6_' ~ time ~ rand;

  my $fh = open $file, :w;
  $fh.print("To: {%mail<To>}\n");
  $fh.print("From: {%mail<From>}\n");
  $fh.print("Subject: {%mail<Subject>}\n");
  $fh.print("\n");
  $fh.print(%mail<Text>);
  $fh.close;

  shell("/usr/lib/sendmail -t < {$file}");

  unlink $file;

  return ;
}

sendmail(
  To      => 'foo@perl6maven.com',
  From    => 'bar@perl6maven.com',
  Subject => 'Reading the Perl 6 Maven',
  Text    => "
Hi,

how are you?

I have been reading the Perl 6 Maven site for a while,
I am even subscribed to the mailing list. Try it!

http://perl6maven.com/

This message was sent using the script I took from
http://perl6maven.com/sending-email-using-perl6

",
);

If you try this script, please change the To and From fields before running it!

Disclaimer

This script only works on Unix-ish systems where the local mail delivery system has been properly configured. It is also very likely that many sites will reject the message if you are running this on your home network, but I managed to send a message to my own addresses. Running it on a server will probably work.

Warning

Do NOT use this subroutine in an environment where any random person can supply the input fields. Be extremely cautious accepting input from the Internet and then using it for sending e-mail with this subroutine.

The explanation

We declare a subroutine and slurp any parameters into a hash called %mail.

sub sendmail(*%mail) {

Go over the 4 required fields and check if all of them were supplied. At this point we don't make any special check for the validity of the addresses. That could be a nice extra feature.

  die "Missing $_" if not %mail{$_} for <To From Subject Text> ;

Create a temporary file using the current timestamp and a random number. As I wrote, it is not perfect but it works for now.

  my $file = '/tmp/perl6_' ~ time ~ rand;

Open the file for writing and write the email. First the headers. Then an empty row. Then the body of the messages.

  my $fh = open $file, :w;
  $fh.print("To: {%mail<To>}\n");
  $fh.print("From: {%mail<From>}\n");
  $fh.print("Subject: {%mail<Subject>}\n");
  $fh.print("\n");
  $fh.print(%mail<Text>);
  $fh.close;

It will look like this:

To: foo@perl6maven.com
From: bar@perl6maven.com
Subject: Reading the Perl 6 Maven

hi,
...

The shell() function can execute an external command. In this case we run the sendmail program of the operating system and redirect the file to its standard input. This call puts the e-mail in the outgoing queue of the local mailing system that will deliver it when time permits.

  shell("/usr/lib/sendmail -t < {$file}");

We get rid of the temporary file:

  unlink $file;

Conclusion

As I wrote, this is not perfect, but if you'd like to build a system that sends out email messages, this can be used. At least on Unix-ish systems.


The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Email:
Full name:
This is a newsletter temporarily running on my personal site (szabgab.com) using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2012-09-12



Comments

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.
Email:
Name: