Sending email notifications via Gmail or other remote SMTP server

Our mail is handled by Gmail, so we don't have own mail-server. PicoLisp has nice mail function, but it turned out that it doesn't support extended SMTP protocol (authentication), so I've found that curl can help us.

Don't forget to create ".netrc" file in the home directory of your application's user. See https://curl.haxx.se/docs/manpage.html#-n for the additional info
> cat ~/.netrc
machine smtp.gmail.com login myaddr@example.com password my_very_secure_password
TODO: this function doesn't provide convenient way to send attachments. However, you can form multipart/mixed content by yourself.

--- app/mail.l ---
# send email via secure SMTP connection
# 'To'-parameter can be string or list (several destinations)
# 'Prg' should print content
(de _mail (ReplyTo To Subj ContentType Prg)
   # curl --url 'smtps://smtp.gmail.com:465' --ssl-reqd 
   #   --mail-from 'myaddr@example.com' --mail-rcpt 'dest@example.com' 
   #   --upload-file er.l --insecure 
   #   -n # use ~/.netrc for login-password
   (let (Body (tmp "mail.txt") Curl NIL CurlRes NIL)
      (out Body
         (prinl "From: " "myaddr@example.com" "^M")
         (prinl "Reply-To: " (if ReplyTo @ "myaddr@example.com") "^M")
         (prinl "To: " (if (atom To) To (glue ", " To)) "^M")
         (prinl "Subject: " Subj "^M")
         (prinl "Content-Type: " ContentType "^M")
         (prinl "^M")
         (run Prg 2) )
      (setq Curl
         (make
            (link "curl"
               "-sS"
               "--url" "smtps://smtp.gmail.com:465" "--ssl-reqd"
               "--mail-from" "'myaddr@example.com"
               "--upload-file" Body
               "-n"
               "--insecure" )
            (if (atom To)
               (link "--mail-rcpt" To)
               (for X To
                  (link "--mail-rcpt" X) ) ) ) )
      (ifn (apply 'call Curl)
         (msg 'ERROR " can't send email to " To) )
      (call "rm" Body) ) )

# send email with HTML-content
# 'To' can be string or list (several destinations)
(de mail_html (ReplyTo To Subj . Prg)
   (_mail ReplyTo To Subj "text/html; charset=utf-8;" Prg) )
Example usage:
(mail_html "me@example.com" MailLst "Hello from PicoLisp application"
   (<p> NIL "Hello!")
   (<p> NIL "Best regards") )

https://picolisp.com/wiki/?gmail

07apr18    m_mans