Reputation: 43
I've been reading through the documentation for Network.Mail.SMTP to send an e-mail. My problem comes after the code begins to run I get a message saying
socket 11: Data.ByteString.hGetLine end of file
When I check the inbox folder for the recipient's email and the sent folder of the sending email, there is no message being sent. What can I do to make this code do it's job. I've posted the code and omitted some of the sensitive sections.
import Network.Mail.Mime
import Network.Mail.SMTP
import qualified Data.Text as T
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
mFrom = Address (Just $ T.pack "FirstName LastName") (T.pack "Sender's Email")
mTo = [Address Nothing (T.pack "Recipient Email")]
mCC = []
mBCC = []
mHeader = [(B.empty, (T.pack "Test Header"))]
pType = T.empty
pEncoding = None
pFilename = Nothing
pHeader = [(B.empty, T.pack "Test Part Header")]
pContent = BL.empty
pPart = Part pType pEncoding pFilename pHeader pContent
alternative =[pPart]
mParts = [alternative]
mMail = Mail mFrom mTo mCC mBCC mHeader mParts
a = sendMailWithLogin' "smtp.gmail.com" 465 "Sender Email" "Sender Password" mMail
main = a
Upvotes: 2
Views: 198
Reputation: 15959
I could reproduce the error, but honestly I don't know why this is happening, maybe it has to do with encryption, which I guess google forbids not to have (but that is just a guess).
But since I remembered having sent some email with haskell in the past - I dug out some old code: note this uses HaskellNet
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Monad (when)
import Control.Exception (bracket)
import Network.HaskellNet.SMTP.SSL
main :: IO ()
main = bracket
(connectSMTPSSL "smtp.gmail.com")
closeSMTP $ \conn ->
do success <- authenticate LOGIN
"[email protected]"
"password"
conn
when success
$ sendPlainTextMail "to" "from" "Test" "test" conn
the usage is pretty straightforward establish a connection, authenticate over it send your email, if you have attachments use sendMimeMail
or sendMimeMail'
or if you want to use the existing Mail
type you already built use sendMimeMail2
.
If you haven't seen bracket
it is the (quite elegant) haskell variant of try..finally
- it simply makes sure that the connection is closed.
Upvotes: 1