Haskell: SMTP over SSL

I wanted to write a utility script in Haskell last night that sent some emails using my gmail account.

However, gmail requires a SSL/TLS connection to their SMTP server, and I couldn't figure out how to accomplish this with HaskellNet's sendMail.

How could I have done this with HaskellNet? Or is there a different SMTP library I should use?

This is quite an old post now, but just thought I'd pop an answer up in case somebody stumbles across it looking for SSL/TLS support for IMAP/SMTP. I've put a library up on Hackage which adds TLS support to HaskellNet's mail functionality by doing pretty much what hammar suggested in his answer.

You can find the library here: http://hackage.haskell.org/package/HaskellNet-SSL along with sample code here.

HaskellNet-SSL: Helpers to connect to SSL/TLS mail servers with , This package ties together the HaskellNet and connection packages to make it easy to open IMAP and SMTP connections over SSL. HaskellNet-SSL. This package ties together the excellent HaskellNet and connection packages to make it easy to open IMAP and SMTP connections over SSL. This is a simple "glue" library; all credit for a) connecting to IMAP/SMTP servers and b) making an SSL connection goes to the aforementioned libraries.

From a quick glance at the docs, I noticed connectStream, which lets you use an already-opened stream, as well as the fact that all the functions work on BSStream s => s rather than a plain Handle. I'm guessing you could use this with the tls package to write a BSStream instance for TLSCtx a (or a newtype) to allow you to use a TLS/SSL connection with HaskellNet's sendMail.

I don't have any experience with either package, but it looks like it might be possible.

Network.HaskellNet.SMTP.SSL, HaskellNet-SSL-0.3.3.0: Helpers to connect to SSL/TLS mail servers with Network.HaskellNet.SMTP.SSL. Contents. Establishing connection; Other Useful � Source; Contents; Index; HaskellNet-SSL-0.2.1: Helpers to connect to SSL/TLS mail servers with HaskellNet

Have you considered configuring sendmail with GMail and using smtp-mail to talk to sendmail?

SMTP, SSL and Haskell, I had to find a way to connect to SMTP server using SSL and only then to send a message. A lot of searching, mostly on Stack Overflow finally� HaskellNet-SSL. This package ties together the excellent HaskellNet and connection packages to make it easy to open IMAP and SMTP connections over SSL. This is a simple "glue" library; all credit for a) connecting to IMAP/SMTP servers and b) making an SSL connection goes to the aforementioned libraries.

The following code works for me on Windows 10

It uses the hackage HaskellNet-SSL

Most of the code is courtesy of czyzykowski

I need to configure google account to allow less secure apps so it's not perfect, but works.

{-# LANGUAGE OverloadedStrings #-}
module Email where

import           Control.Monad
import           Data.ByteString.Lazy        (toStrict)
import           Data.Text                   (unpack)
import           Network.HaskellNet.Auth
import           Network.HaskellNet.SMTP
import           Network.HaskellNet.SMTP.SSL
import           Network.Mail.Mime

toString :: Address -> String
toString Address { addressEmail = email } = unpack email

defaultMail :: Mail
defaultMail = Mail {
                            mailFrom = Address (Just "My Name" ) "me@example.com",
                            mailTo =[Address (Just "Your Name") "someone@example.com"],
                            mailCc = [],
                            mailBcc = [],
                            mailHeaders = [("Subject", "Haskell Rocks")],
                            mailParts = []
                                                    }
sendEmail msg = do
  rendered   <- renderMail' defaultMail {mailParts = [[plainPart msg]]}
  doSMTPSTARTTLSWithSettings  "smtp.gmail.com" defaultSettingsSMTPSSL{sslPort = 587} $ \connection -> do
      succeeded  <- authenticate LOGIN
                                 "mygoogleaccount@gmail.com"
                                 "mypassword"
                                 connection
      when succeeded $ do
          putStrLn "conntected. Now we'll send the email"
          sendMail (toString (mailFrom defaultMail))
                   (map toString (mailTo defaultMail))
                   (toStrict rendered) connection

Network.HaskellNet.SMTP, NOTE: For SSL/TLS support you may establish the connection using the functions (such as connectSMTPSSL ) provided in the Network.HaskellNet.SMTP . You will encounter authentication errors if you try to connect to an SMTP server that expects SSL. If that's what you're looking to do, try HaskellNet-SSL. Thanks. This library is based on code from HaskellNet, which appears to be no longer maintained.

🤵 🤨 Haskell: SMTP over SSL, This is a pretty old post now, but I just thought that I would drink the answer in case someone stumbles upon it, looking for SSL / TLS support for IMAP / SMTP. SMTP settings are only for sending email; you'll also need to provide the settings for receiving emails. Receiving mail is done through POP3 or IMAP servers. Before you identify those settings in your email client, enable access through settings within Gmail by going to Settings > Forwarding and POP/IMAP .

How to Interact with Gmail over SMTP : haskellquestions, I tried a combination of HaskellNet and HaskellNet-SSL, but I can't get the authentication to work. I am using an app-specific password since I do have 2FA, but I� SSL (Secure Sockets Layer) and its successor, Transport Layer Security (TLS), provide a way to encrypt a communication channel between two computers over the Internet. In most cases, the terms SSL and TLS can be used interchangeably unless you’re referring to a specific version of the protocol.

dpwright/HaskellNet-SSL: Helpers to connect to SSL/TLS , This package ties together the excellent HaskellNet and connection packages to make it easy to open IMAP and SMTP connections over SSL. This is a simple� SMTP is an acronym for Simple Mail Transfer Protocol. It is an Internet standard for electronic mail (e-mail) transmission across Internet Protocol (IP) networks. SMTP uses TCP port 25. SMTP connections secured by SSL are known by the shorthand SMTPS, though SMTPS is not a protocol in its own right

Comments
  • Did you manage to find the solution?
  • Salil: nah, I gave up. hammar's solution might work.
  • There is also some discussion on Haskell-beginners but it's not a complete solution.
  • replace tsl with TLS. TLS is "transport level security". SSL is "secure socket layer"
  • I could I guess. How to use Network.TLS for a password-authenticated connection isn't jumping out at me, but I'll stare at it a bit more.