Send yourself a greetings email from me

,

Every new year, on January 1st, a good friend of mine sends automated greeting emails at midnight sharp. However, this year, he forgot to set the subject correctly. While the message body appropriately contains “Meilleurs vœux à toutes et à tous pour 2011 !” (Best wishes to all for 2011!), the mail subject reads “Bonne année deux mille dix !” (Happy new year two thousand and ten!).

Fortunately, Factor will allow me to send similar greeting emails without making the same mistake. It is easy to translate an integer value such as “2011” into a French string (“deux mille onze”) by using the math.text.french vocabulary that I wrote in 2009, based on math.text.english from Aaron Schaefer and a lot of complicated French language rules.

The following code lets me send a mail to a targeted recipient containing the correct year both in the subject and in the body. I hope my friend does not mind that I stole the text from his email.

USING: accessors arrays calendar combinators io.encodings.utf8
io.sockets kernel make math.text.french math.parser namespaces smtp ;
IN: newyear

: subject ( -- string )
    [ "Bonne année " % now year>> number>text % " !" % ] "" make ;

: body ( -- string )
    [
       "Meilleurs vœux à toutes et à tous pour " %
        now year>> number>string %
        " ! Que cette année soit pour\n" %
        "tout le monde riche d'heureuses " %
        "surprises en toutes choses !\n\n" %
        "Amitiés,\nSamuel" %
    ] "" make ;

: new-email ( recipients sender -- email )
    <email>
      [ from<< ] keep
      [ to<< ] keep
      subject >>subject
      body >>body ;

: send-greetings-from-sam ( email-address smtp-host -- )
    25 <inet> smtp-server set
    [
        1array "Samuel Tardieu <sam@rfc1149.net>" new-email
        send-email
    ] with-smtp-connection ;

If you want to try it, you can copy-paste it into a Factor listener and run

"Your name <your-email-address>" "your-smtp-host"
send-greetings-from-sam

to receive greetings from me.

If everything goes well, you should receive something like this in your mailbox:

From: Samuel Tardieu <sam@rfc1149.net>
To: Your name <your-email-address>
Subject: Bonne année deux mille onze !
Date: Sat, 1 Jan 2011 01:24:49 +0100
Message-Id: <15391953945985082941-1293841489599214@spyke>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8

Meilleurs vœux à toutes et à tous pour 2011 ! Que cette année soit pour
tout le monde riche d'heureuses surprises en toutes choses !

Amitiés,
Samuel

Do not hesitate to run this every year. I mean it.

If you like this post, you can send some bitcoin dust to 1Bo78aNzJvkmeLTw8aptaFipvRWyNQP2WF (or click here).
blog comments powered by Disqus