HaskellNet-0.4.3: Client support for POP3, SMTP, and IMAP

Safe HaskellNone
LanguageHaskell98

Network.HaskellNet.SMTP

Contents

Description

This module provides functions for working with the SMTP protocol in the client side, including opening and closing connections, sending commands to the server, authenticate and sending mails.

Here's a basic usage example:

import Network.HaskellNet.SMTP
import Network.HaskellNet.Auth
import qualified Data.Text.Lazy as T

main = doSMTP "your.smtp.server.com" $ \conn ->
   authSucceed <- authenticate PLAIN "username" "password" conn
   if authSucceed
       then sendPlainTextMail "receiver@server.com" "sender@server.com" "subject" (T.pack "Hello! This is the mail body!") conn
       else print "Authentication failed."

Notes for the above example:

  • First the SMTPConnection is opened with the doSMTP function. The connection should also be established with functions such as connectSMTP, connectSMTPPort and doSMTPPort. With the doSMTP* functions the connection is opened, then executed an action with it and then closed automatically. If the connection is opened with the connectSMTP* functions you may want to close it with the closeSMTP function after using it. It is also possible to create a SMTPConnection from an already opened connection stream (BSStream) using the connectStream or doSMTPStream functions.

    NOTE: For SSL/TLS support you may establish the connection using the functions (such as connectSMTPSSL) provided in the Network.HaskellNet.SMTP.SSL module of the HaskellNet-SSL package.

  • The authenticate function authenticates to the server with the specified AuthType. PLAIN, LOGIN and CRAM_MD5 AuthTypes are available. It returns a Bool indicating either the authentication succeed or not.
  • To send a mail you can use sendPlainTextMail for plain text mail, or sendMimeMail for mime mail.

Synopsis

Types

Establishing Connection

connectSMTPPort Source

Arguments

:: String

name of the server

-> PortNumber

port number

-> IO SMTPConnection 

connecting SMTP server with the specified name and port number.

connectSMTP Source

Arguments

:: String

name of the server

-> IO SMTPConnection 

connecting SMTP server with the specified name and port 25.

connectStream :: BSStream -> IO SMTPConnection Source

create SMTPConnection from already connected Stream

Operation to a Connection

sendCommand :: SMTPConnection -> Command -> IO (ReplyCode, ByteString) Source

send a method to a server

closeSMTP :: SMTPConnection -> IO () Source

close the connection. This function send the QUIT method, so you do not have to QUIT method explicitly.

Other Useful Operations

authenticate :: AuthType -> UserName -> Password -> SMTPConnection -> IO Bool Source

This function will return True if the authentication succeeds. Here's an example of sending a mail with a server that requires authentication:

   authSucceed <- authenticate PLAIN "username" "password" conn
   if authSucceed
       then sendPlainTextMail "receiver@server.com" "sender@server.com" "subject" (T.pack "Hello!") conn
       else print "Authentication failed."

sendMail Source

Arguments

:: String

sender mail

-> [String]

receivers

-> ByteString

data

-> SMTPConnection 
-> IO () 

sending a mail to a server. This is achieved by sendMessage. If something is wrong, it raises an IOexception.

doSMTPPort :: String -> PortNumber -> (SMTPConnection -> IO a) -> IO a Source

doSMTPPort open a connection, and do an IO action with the connection, and then close it.

doSMTP :: String -> (SMTPConnection -> IO a) -> IO a Source

doSMTP is similar to doSMTPPort, except that it does not require port number but connects to the server with port 25.

doSMTPStream :: BSStream -> (SMTPConnection -> IO a) -> IO a Source

doSMTPStream is similar to doSMTPPort, except that its argument is a Stream data instead of hostname and port number.

sendPlainTextMail Source

Arguments

:: String

receiver

-> String

sender

-> String

subject

-> Text

body

-> SMTPConnection

the connection

-> IO () 

Send a plain text mail.

sendMimeMail Source

Arguments

:: String

receiver

-> String

sender

-> String

subject

-> Text

plain text body

-> Text

html body

-> [(Text, FilePath)]

attachments: [(content_type, path)]

-> SMTPConnection 
-> IO () 

Send a mime mail. The attachments are included with the file path.

sendMimeMail' Source

Arguments

:: String

receiver

-> String

sender

-> String

subject

-> Text

plain text body

-> Text

html body

-> [(Text, Text, ByteString)]

attachments: [(content_type, file_name, content)]

-> SMTPConnection 
-> IO () 

Send a mime mail. The attachments are included with in-memory ByteString.