Wednesday 23 January 2013

Emailer

Not many users know that rebol has a ready to use interface for writing and sending email: emailer.
You have to put this command inside a layout to work it properly,otherwise it will stuck.

Here an example:
view layout [btn "click to launch emailer" [ emailer ]]


However you have to set your email information correctly. Usually when you launch viewtop for the first time, it asks your email settings.
If you want to change your email settings, just launch set-user, a wonderful pop-up will appear:

You probably noted that you can't set POP and SMTP passwords from set-user interface. Well, you can set them manually with 4 lines of code:


system/schemes/pop/user:   "my user name for POP"
system/schemes/pop/pass:   "mypassword for POP"
system/schemes/esmtp/user:   "my user name for SMTP"
system/schemes/esmtp/pass:   "mypassword for SMTP"


If you are a smart rebol user (and you are smart reading this blog), your next step will be to set this way:

system/schemes/pop/pass: func [] [request-pass/title/only "POP password?"]
system/schemes/esmtp/pass: func [] [request-pass/title/only "SMTP password?"]


In order to read your email message, just launch:

my-emails: read pop://


my-emails will be a block of string each string is an email message. Here a mailbox with just a message:
[ {Return-Path: <angerangel@gmail.com>
Received: from imp-6.mail.tiscali.it (10.39.115.227) by mx-3-it.mail.tiscali.it (8.5.148)
        id 50F69CF201DA6639 for maxint@tiscali.it; Wed, 23 Jan 2013 12:42:03 +0100
Received: from mail-lb0-f177.google.com ([209.85.217.177])
^-by imp-6.mail.tiscali.it with
^-id rPi31k00l3qDwFD01Pi3fM; Wed, 23 Jan 2013 12:42:03 +0100
x-cnfs-analysis: v=2.0 cv=FraHDhXq c=1 sm=2 a=nDghuxUhq_wA:10
a=pGLkceISAAAA:8 a=yR1Yy4l2QB8A:10 a=bi5fCUJXM4t3MQuAs5MA:9 a=wPNLvfGTeEIA:10
a=oct3qPWNAAAA:8 a=djclfNPk0vczJORTAykA:9 a=bJZN8nw/mWW4J2Uhyp6xUg==:117
Received: by mail-lb0-f177.google.com with SMTP id go11so2087218lbb.22
        for <maxint@tiscali.it>; Wed, 23 Jan 2013 03:41:47 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=mime-version:x-received:date:message-id:subject:from:to
        :content-type;
        bh=gP//j44iPGJ7Wqt7gjsReghpfyJhjSivP8SzCZqyOCk=;
        b=apQPYsfSl5Lceaj4zd7jq2BrS7VYxv8cphquOdrRqf6fhBEqT4bsdiJLb2P7turR7C
        WPTK3Fb3IUJDez0Q/dajGztOjYi3PlKCI1NzFFI/XimwhVHiRaB4ZpuhsOj4F2r1ecWL
        WWXjkLcRRiqFACUl5vbvY5cVyzk9Hk6qqaZWaryD0iSEmFWHliWSWusiYZTWkWo5dQny
        pZ4IkIfEAhIT6a6ySJ0+2sMqQ0Vhouq1qdN3OU/eXSx9So3P145S1Vq/mEoCzkYUJBwL
        6r4qTNDaY0xbBPQT6K6cBTeg4eMAoglUiRhq660M7JpEs2iKteqy2F5gI8tk1l1960Rm
        kCbA==
MIME-Version: 1.0
X-Received: by 10.152.121.212 with SMTP id lm20mr1026251lab.42.1358941307899;
Wed, 23 Jan 2013 03:41:47 -0800 (PST)
Received: by 10.114.12.133 with HTTP; Wed, 23 Jan 2013 03:41:47 -0800 (PST)
Date: Wed, 23 Jan 2013 12:41:47 +0100
Message-ID: <CAJ1MPLX2uj2OxCqUPrWrjvgwp76r832=TpV4qncGbV9wDje01w@mail.gmail.com>
Subject: Prova rebol
From: Max Angel <angerangel@gmail.com>
To: maxint@tiscali.it
Content-Type: multipart/alternative; boundary=f46d04374ee1ed8f5004d3f32dd2
--f46d04374ee1ed8f5004d3f32dd2
Content-Type: text/plain; charset=ISO-8859-1
Testo di prova
--
--f46d04374ee1ed8f5004d3f32dd2
Content-Type: text/html; charset=ISO-8859-1
Testo di prova<br clear="all"><br>-- <br><img src="https://linuxcounter.net/cert/314257.png"><br>
--f46d04374ee1ed8f5004d3f32dd2--
}
]

How to read it easily? There is the fantastic function import-email, look here:

>>mymessage: import-email my-emails/1
>> ? mymessage
mymessage is an object of value:
   To              block!    length: 1
   CC              none!     none
   BCC             none!     none
   From            block!    length: 1
   Reply-To        none!     none
   Date            date!     23-Jan-2013/12:41:47+1:00
   Subject         string!   "Prova rebol"
   Return-Path     string!   ""
   Organization    none!     none
   Message-Id      string!   {CAJ1MPLX2uj2OxCqUPrWrjvgwp76r832=TpV4qncGbV9wDje...
   Comment         none!     none
   X-REBOL         string!   "View 2.7.8.3.1 http://WWW.REBOL.COM"
   MIME-Version    string!   "1.0"
   Content-Type    string!   {multipart/alternative; boundary=f46d04374ee1ed8f5...
   Content         string!   {--f46d04374ee1ed8f5004d3f32dd2 Content-Type: text...
   Received        block!    length: 4
   x-cnfs-analysis string!   {v=2.0 cv=FraHDhXq c=1 sm=2 a=nDghuxUhq_wA:10 a=pG...
   DKIM-Signature  string!   {v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com...
   X-Received      string!   {by 10.152.121.212 with SMTP id lm20mr1026251lab.4...



So you can read your message easily:


with this code:

view layout [
    across
    label "Subject:"
    text bold mymessage/subject
    return
    label "From"    
    text form mymessage/from
    return
    label "To:"
    text   form mymessage/to
    return
    label "Date:"
    text   form mymessage/date
    return
    box black 200x1
    do [   ;the following lines are to extraxt just the plain text version of the email message
        mex:   copy " "
        parse mymessage/content   [thru   "Content-Type: text/plain;" copy temp to " -- "   (append mex temp)]
        mex: remove (parse mex none)
        ]
        return
    text   form mex
    ]


As you notice, making an email client with rebol is a breeze. (Image to use list in order to list all your emails, and your obtain an email client better than thunderbird... :-) )

1 comment:

  1. Nice article.

    In addition, I have enhanced the emailer window included in the viewtop to allow attachment.

    See: http://www.rebol.org/view-script.r?script=vt-emailer.r

    ReplyDelete