Safe Haskell | None |
---|---|
Language | Haskell2010 |
Interactions with GMail made simple.
Sending mails
For now, only mail sending is implemented. Here's an example:
First we read the Google key file.
do gkey <- readKeyFile "google-key.json"
Then we start a session. We provide the mail address of the user that will send the mail.
session <- newSession gkey "sender@example.com"
Here's the mail description.
let mail :: Mail Text mail = Mail { mail_sender = "Me" , mail_recipient = "recipient@example.com" , mail_subject = "Example mail" , mail_body = "This is an example mail." }
Finally, we simply send the mail.
sendMail session mail
That's it.
Importing this library
I would recommend importing this module qualified. For example:
import qualified Network.GMail.Simple as GMail
Synopsis
- data Key = Key {}
- readKeyFile :: FilePath -> IO Key
- data Session
- newSession :: Key -> MailAddress -> IO Session
- newtype MailAddress = MailAddress Text
- data Mail a = Mail {
- mail_sender :: Text
- mail_recipient :: MailAddress
- mail_subject :: Text
- mail_body :: a
- sendMail :: ToMailBody a => Session -> Mail a -> IO ()
- class ToMailBody a where
- toMailBody :: a -> Text
- mailContentType :: proxy a -> MediaType
- data GMailException
Key
Google API Key from a service account. You can create one
inside your project in Google Cloud.
Once you have it, you can download it to a file and read it using readKeyFile
.
readKeyFile :: FilePath -> IO Key Source #
Read the key file provided by Google Cloud.
It throws a FailedToParseKey
exception when
the file fails to parse.
If you don't want to read it from a local file,
you can use the FromJSON
instance of Key
to read it. The function readKeyFile
is just a
wrapper around that.
Session
A session that can be used to send mails.
- It may be reused.
- Multiple threads can use it simultaneously.
:: Key | Google API key |
-> MailAddress | Mail address of the sender |
-> IO Session |
Create a new session for the given sender.
newtype MailAddress Source #
A mail address as text.
Instances
IsString MailAddress Source # | |
Defined in Network.GMail.Simple fromString :: String -> MailAddress # |
Mail datatype.
|
sendMail :: ToMailBody a => Session -> Mail a -> IO () Source #
Send mail using a session. It might throw a FailedToSend
exception.
In order for this to work, the user must have permissions for the https://www.googleapis.com/auth/gmail.send
scope.
Mail body
class ToMailBody a where Source #
Class of types that can be used as mail body.
toMailBody :: a -> Text Source #
Textual representation of the mail body.
mailContentType :: proxy a -> MediaType Source #
Value for the Content-Type
header.
Instances
ToMailBody Text Source # | |
Defined in Network.GMail.Simple toMailBody :: Text -> Text Source # mailContentType :: proxy Text -> MediaType Source # | |
ToMailBody Html Source # | |
Defined in Network.GMail.Simple toMailBody :: Html -> Text Source # mailContentType :: proxy Html -> MediaType Source # |
Exceptions
data GMailException Source #
Exceptions thrown by functions in this library.
FailedToSend Value | A mail failed to be sent. The JSON value contains the error message as sent by Google. |
FailedToParseKey String | A key file failed to parse. The string contains the parsing error. |
Instances
Show GMailException Source # | |
Defined in Network.GMail.Simple showsPrec :: Int -> GMailException -> ShowS # show :: GMailException -> String # showList :: [GMailException] -> ShowS # | |
Exception GMailException Source # | |
Defined in Network.GMail.Simple |