Safe Haskell | None |
---|---|
Language | Haskell98 |
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 thedoSMTP
function. The connection should also be established with functions such asconnectSMTP
,connectSMTPPort
anddoSMTPPort
. With thedoSMTP*
functions the connection is opened, then executed an action with it and then closed automatically. If the connection is opened with theconnectSMTP*
functions you may want to close it with thecloseSMTP
function after using it. It is also possible to create aSMTPConnection
from an already opened connection stream (BSStream
) using theconnectStream
ordoSMTPStream
functions.NOTE: For SSL/TLS support you may establish the connection using the functions (such as
connectSMTPSSL
) provided in theNetwork.HaskellNet.SMTP.SSL
module of the HaskellNet-SSL package.- The
authenticate
function authenticates to the server with the specifiedAuthType
.PLAIN
,LOGIN
andCRAM_MD5
AuthType
s are available. It returns aBool
indicating either the authentication succeed or not. - To send a mail you can use
sendPlainTextMail
for plain text mail, orsendMimeMail
for mime mail.
- data Command
- data Response
- = Ok
- | SystemStatus
- | HelpMessage
- | ServiceReady
- | ServiceClosing
- | UserNotLocal
- | CannotVerify
- | StartMailInput
- | ServiceNotAvailable
- | MailboxUnavailable
- | ErrorInProcessing
- | InsufficientSystemStorage
- | SyntaxError
- | ParameterError
- | CommandNotImplemented
- | BadSequence
- | ParameterNotImplemented
- | MailboxUnavailableError
- | UserNotLocalError
- | ExceededStorage
- | MailboxNotAllowed
- | TransactionFailed
- data AuthType
- data SMTPConnection
- connectSMTPPort :: String -> PortNumber -> IO SMTPConnection
- connectSMTP :: String -> IO SMTPConnection
- connectStream :: BSStream -> IO SMTPConnection
- sendCommand :: SMTPConnection -> Command -> IO (ReplyCode, ByteString)
- closeSMTP :: SMTPConnection -> IO ()
- authenticate :: AuthType -> UserName -> Password -> SMTPConnection -> IO Bool
- sendMail :: String -> [String] -> ByteString -> SMTPConnection -> IO ()
- doSMTPPort :: String -> PortNumber -> (SMTPConnection -> IO a) -> IO a
- doSMTP :: String -> (SMTPConnection -> IO a) -> IO a
- doSMTPStream :: BSStream -> (SMTPConnection -> IO a) -> IO a
- sendPlainTextMail :: String -> String -> String -> Text -> SMTPConnection -> IO ()
- sendMimeMail :: String -> String -> String -> Text -> Text -> [(Text, FilePath)] -> SMTPConnection -> IO ()
- sendMimeMail' :: String -> String -> String -> Text -> Text -> [(Text, Text, ByteString)] -> SMTPConnection -> IO ()
- sendMimeMail2 :: Mail -> SMTPConnection -> IO ()
Types
data SMTPConnection Source
Establishing Connection
:: String | name of the server |
-> PortNumber | port number |
-> IO SMTPConnection |
connecting SMTP server with the specified name and port number.
:: 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."
:: 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.
Send a plain text mail.
:: 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.
:: 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
.
sendMimeMail2 :: Mail -> SMTPConnection -> IO () Source