SMTP email

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{-# LANGUAGE OverloadedStrings #-}
module Helper.Mail where

import System.IO
import Control.Monad
import qualified Data.Text as T
import Data.Maybe
import Data.Word (Word16)

import Network.HaskellNet.SMTP
import Network.HaskellNet.Auth
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
import Network.Socket.Internal

import qualified Network.SMTP.Simple as Simple

data SmtpInfo = SmtpInfo {
    host :: String,
    port :: Int,
    userName :: Maybe String,
    password :: Maybe String
}

data Message = Message {
    from :: T.Text,
    to :: T.Text,
    subject :: T.Text,
    body :: T.Text
}

testInfo = SmtpInfo {
    host = "127.0.0.1",
    port = 25,
    userName = Nothing,
    password = Nothing
}

testMessage = Message {
    from = "info@example.com",
    to = "test@example.com",
    subject = "Test email",
    body = "This is a test email"
}

sendTest :: IO ()
sendTest = sendEmail testInfo testMessage


sendEmail :: SmtpInfo -> Message -> IO ()
sendEmail info message = do
    let sender = (T.unpack $ from message)
        receivers = [(T.unpack $ to message)]
        msg = B8.pack ("Subject: " ++ (T.unpack $ subject message) ++ "\\n" ++ (T.unpack $ body message))
        user = (userName info)
        pass = (password info)

    doSMTPPort (host info) (Network.Socket.Internal.PortNum $ fromIntegral $ port info) $ (\h -> do
        sendCommand h (EHLO "nschoe")
        when (isJust user && isJust pass) $ do
            sendCommand h (AUTH LOGIN (fromJust user) (fromJust pass))
            return ()
        sendCommand h (MAIL sender)
        mapM (sendCommand h . RCPT) receivers
        (code, rep1) <- sendCommand h (DATA msg)
        putStrLn $ "Code: " ++ show code ++ "\nMessage: " ++ show rep1
        return ()

        )
    return ()

sendClientEmail :: SmtpInfo -> Message -> IO ()
sendClientEmail info message = do
    Simple.sendSimpleMessages (hPutStrLn stderr) (host info) ("example.com") [mess]
    where
        mess = Simple.SimpleMessage
            [Simple.NameAddr Nothing (T.unpack $ from message)]
            [Simple.NameAddr Nothing (T.unpack $ to message)]
            (T.unpack $ subject message)
            (T.unpack $ body message)
    
53:21: Warning: Redundant bracket
Found:
[(T.unpack $ to message)]
Why not:
[T.unpack $ to message]
54:39: Warning: Redundant $
Found:
(T.unpack $ subject message) ++ "\\n" ++ (T.unpack $ body message)
Why not:
T.unpack (subject message) ++ "\\n" ++ (T.unpack $ body message)
54:71: Warning: Redundant $
Found:
"\\n" ++ (T.unpack $ body message)
Why not:
"\\n" ++ T.unpack (body message)
58:5: Warning: Redundant $
Found:
doSMTPPort (host info)
(Network.Socket.Internal.PortNum $ fromIntegral $ port info)
$
(\ h ->
do sendCommand h (EHLO "nschoe")
when (isJust user && isJust pass) $
do sendCommand h (AUTH LOGIN (fromJust user) (fromJust pass))
return ()
sendCommand h (MAIL sender)
mapM (sendCommand h . RCPT) receivers
(code, rep1) <- sendCommand h (DATA msg)
putStrLn $ "Code: " ++ show code ++ "\nMessage: " ++ show rep1
return ())
Why not:
doSMTPPort (host info)
(Network.Socket.Internal.PortNum $ fromIntegral $ port info)
(\ h ->
do sendCommand h (EHLO "nschoe")
when (isJust user && isJust pass) $
do sendCommand h (AUTH LOGIN (fromJust user) (fromJust pass))
return ()
sendCommand h (MAIL sender)
mapM (sendCommand h . RCPT) receivers
(code, rep1) <- sendCommand h (DATA msg)
putStrLn $ "Code: " ++ show code ++ "\nMessage: " ++ show rep1
return ())
64:9: Error: Use mapM_
Found:
mapM (sendCommand h . RCPT) receivers
Why not:
mapM_ (sendCommand h . RCPT) receivers
73:32: Error: Redundant do
Found:
do Simple.sendSimpleMessages (hPutStrLn stderr) (host info)
("example.com")
[mess]
Why not:
Simple.sendSimpleMessages (hPutStrLn stderr) (host info)
("example.com")
[mess]
74:62: Error: Redundant bracket
Found:
("example.com")
Why not:
"example.com"