rampion
rampion

Reputation: 89093

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?

Upvotes: 8

Views: 1326

Answers (4)

McBear Holden
McBear Holden

Reputation: 833

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" ) "[email protected]",
                            mailTo =[Address (Just "Your Name") "[email protected]"],
                            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
                                 "[email protected]"
                                 "mypassword"
                                 connection
      when succeeded $ do
          putStrLn "conntected. Now we'll send the email"
          sendMail (toString (mailFrom defaultMail))
                   (map toString (mailTo defaultMail))
                   (toStrict rendered) connection

Upvotes: 1

danielpwright
danielpwright

Reputation: 657

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.

Upvotes: 7

Wilfred Hughes
Wilfred Hughes

Reputation: 31171

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

Upvotes: 1

hammar
hammar

Reputation: 139890

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.

Upvotes: 3

Related Questions