Examples of sending HTML email w/ Racket?

88 views
Skip to first unread message

Brian Adkins

unread,
Apr 8, 2020, 12:54:48 PM4/8/20
to Racket Users
I was able to write a simple wrapper around smtp-send-message and get it working through SendGrid in a few minutes (see below), but I wasn't able to find any examples of sending emails containing both a plain text version and HTML version. Can anyone point me to some examples?

Thanks,
Brian Adkins


#lang racket/base

(require net/head
         net/smtp
         openssl
         racket/contract)
(require "./axio-env.rkt")

;; (module+ main
;;   (send-email "Fred Flintstone <fr...@example.com>"
;;               '("Barney Rubble <bar...@example.com>")
;;               "Test message subject"
;;               (list
;;                "Message line one"
;;                "line two"
;;                ""
;;                "line four")))
(define/contract (send-email from to subject message-lines)
  (-> string? (listof string?) string? (listof (or/c string? bytes?)) any)
  (smtp-send-message smtp-server
                     from
                     to
                     (standard-message-header from to '() '() subject)
                     message-lines
                     #:port-no     smtp-port
                     #:auth-user   smtp-username
                     #:auth-passwd smtp-password
                     #:tls-encode  ports->ssl-ports))

George Neuner

unread,
Apr 8, 2020, 1:46:43 PM4/8/20
to Brian Adkins, racket users

On 4/8/2020 12:54 PM, Brian Adkins wrote:
> I was able to write a simple wrapper around smtp-send-message and get
> it working through SendGrid in a few minutes (see below), but I wasn't
> able to find any examples of sending emails containing both a plain
> text version and HTML version. Can anyone point me to some examples?
>
> Thanks,
> Brian Adkins

You done the hard part.  Once you can send a plain text message, HTML is
not much harder.

You have to construct a MIME multipart message, which has a particular
internal structure.
See:
https://stackoverflow.com/questions/10631856/mime-type-to-satisfy-html-email-images-and-plain-text
http://www.enewsletterpro.com/articles/multi_part_mime_messages.asp
https://www.ietf.org/rfc/rfc1521.txt


My solution is to use message templates like the attached - which shows
using not just multipart but also embedded graphics.  My program
customizes the message by substituting for the '@' preceded "variables"
in the template.  Feel free to (ab)use the same style or adapt it for
your needs.

George


mime-pwd_reset_confirm

Brian Adkins

unread,
Apr 8, 2020, 6:09:14 PM4/8/20
to Racket Users
Thanks for the info George. Are you feeding that entire template (after substituting the @ variables) into the smtp-send-message function as the message argument i.e. a list of lines? I'm also curious about having "subject" in the template vs. the header.

I take it from your reply there isn't already something in existence that streamlines this process in a similar way to a framework such as Rails.

Brian Adkins

unread,
Apr 8, 2020, 6:45:58 PM4/8/20
to Racket Users
I got it to work by stuffing the right values into the header - thanks!
 

George Neuner

unread,
Apr 8, 2020, 9:28:20 PM4/8/20
to Brian Adkins, Racket Users

On 4/8/2020 6:09 PM, Brian Adkins wrote:

Thanks for the info George. Are you feeding that entire template (after substituting the @ variables) into the smtp-send-message function as the message argument i.e. a list of lines?

Yes.


I'm also curious about having "subject" in the template vs. the header.

History.  When I first did this (many years ago) there was an issue with  standard-message-header  screwing up the line breaks, and the resulting message being formatted incorrectly if there was a non-empty subject string.

I am sure this has long been fixed, but at the time I started constructing my own header strings using  insert-field  from the net/head library.  Message[*] headers can be in any order - they need only to precede the body - so it didn't really matter where the subject line came from, and since the subjects were static for most of my messages, it was simplest to just put them into the message templates.

[*] Note that embedded MIME multipart headers DO have defined order.



I take it from your reply there isn't already something in existence that streamlines this process in a similar way to a framework such as Rails.

Don't get me started on Rails  8-(  Makes easy things easy and hard things damn near impossible.


There's nothing in Racket for MIME that I'm aware of ... but then multipart formatting is needed only for multi-version messages, or for embedding non-text data (graphics, etc.) into a message.   Since so many people read mail in web browsers, and most offline clients handle HTML, I don't think many people even bother with multi-version messages any more.  Sending plain HTML is as easy as sending plain text.

Multipart formatting is recursive and parts can be nested arbitrarily, so handling it all properly could be complicated given the need to integrate with other code generating the message body (HTML, etc.) within.  It's doable ... I just never got the urge to tackle it.

George

Hendrik Boom

unread,
Apr 9, 2020, 8:39:56 AM4/9/20
to Racket Users
On Wed, Apr 08, 2020 at 09:28:11PM -0400, George Neuner wrote:

>
> There's nothing in Racket for MIME that I'm aware of ... but then multipart
> formatting is needed only for multi-version messages, or for embedding
> non-text data (graphics, etc.) into a message.   Since so many people read
> mail in web browsers, and most offline clients handle HTML, I don't think
> many people even bother with multi-version messages any more.  Sending plain
> HTML is as easy as sending plain text.

But receiving HTML has security issues. I prefer to avoid it.

I rarely read the HTML parts of multi-version messages.

-- hendrik

Matthew Flatt

unread,
Apr 9, 2020, 8:47:09 AM4/9/20
to George Neuner, Brian Adkins, Racket Users
At Wed, 8 Apr 2020 21:28:11 -0400, George Neuner wrote:
> There's nothing in Racket for MIME that I'm aware of

There's a `net/mime` library.

I'm replying with an attachment so you can see what it generates, since
my email client uses that library.


Matthew
racket-logo.png

Brian Adkins

unread,
Apr 9, 2020, 10:09:21 AM4/9/20
to Racket Users
I looked at the net/mime library, but, as the title of the doc page suggests, it seemed to only be about decoding, not creating:


Are the functions for creating multipart email messages documented elsewhere?

Thanks,
Brian

Matthew Flatt

unread,
Apr 9, 2020, 10:25:03 AM4/9/20
to Brian Adkins, Racket Users
At Thu, 9 Apr 2020 07:09:21 -0700 (PDT), Brian Adkins wrote:
> I looked at the net/mime library, but, as the title of the doc page
> suggests, it seemed to only be about decoding, not creating:
>
> https://docs.racket-lang.org/net/mime.html?q=net%2Fmime

Ah, right. I think I've made this mistake before.


Encoding is be built into SirMail (very old code):

https://github.com/mflatt/sirmail/blob/master/sirmail/sendr.rkt#L136

It would make sense to have a better version of this in a package, of
course.

Philip McGrath

unread,
Apr 9, 2020, 2:37:59 PM4/9/20
to Matthew Flatt, Brian Adkins, Racket Users
I put up an example taken from my code for Digital Ricoeur in response to an old mailing list thread. It is not ready for general-purpose use—in particular, I basically only have to deal with trusted input—but here it is: https://github.com/LiberalArtist/multipart-writing-examples

That thread also has some discussion about what a good package might want to address and some slightly annoying differences between "normal" mime and "multipart/form-data" used for web forms.

-Philip


--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/5e8f303a.1c69fb81.69759.d940SMTPIN_ADDED_MISSING%40gmr-mx.google.com.

mark M

unread,
Jul 31, 2021, 12:41:33 PM7/31/21
to Racket Users
(require "./axio-env.rkt")

is this part of emailing program? as i am unable to install axio env 

raco pkg install g...@github.com:AxioFramework/axio.git
raco pkg install: invalid package source;
inferred package name includes disallowed characters
given: g...@github.com:AxioFramework/axio.git
mark@ll~ $ raco pkg install https://github.com:AxioFramework/axio.git
raco pkg install: invalid package source;
cannot parse URL
given: https://github.com:AxioFramework/axio.git

don't seem to work ; kindly help #noob

write...@gmail.com

unread,
Jul 31, 2021, 3:04:23 PM7/31/21
to Racket Users
made some changes removed need for axio-env.rkt, but i am still unable
to send email
so here follows a #warning noob email question, assuming the smtp
server name password and settings are right is there any reason
http://pastie.org/p/0CduaNLM67JpE7nk9I6vDx/raw does not work . Can
anyone point me to some working examples if possible?

source of program
https://www.mail-archive.com/racket...@googlegroups.com/msg43840.html
> --
> You received this message because you are subscribed to the Google Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/221bf2b2-7ceb-4cba-b483-be2b71c35d0fn%40googlegroups.com.

br...@lojic.com

unread,
Aug 1, 2021, 12:09:46 PM8/1/21
to Racket Users
The axio-env.rkt file is part of the ToDo app I created for RacketCon. You can see the entire ToDo app here:


In particular, axio-env.rkt is meant to live outside of source control because it has secrets. An example file, that would need to be renamed to axio-env.rkt is here:


An example of sending an email can be found in the comment controller here:


Hope this helps a little.

Brian
Reply all
Reply to author
Forward
0 new messages