| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Network.GMail.Simple
Description
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.
Constructors
| Key | |
Fields | |
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.
Arguments
| :: 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.
Constructors
| MailAddress Text |
Instances
| IsString MailAddress Source # | |
Defined in Network.GMail.Simple Methods fromString :: String -> MailAddress # | |
Mail datatype.
Constructors
Fields
| |
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.
Methods
toMailBody :: a -> Text Source #
Textual representation of the mail body.
mailContentType :: proxy a -> MediaType Source #
Value for the Content-Type header.
Instances
| ToMailBody Html Source # | |
Defined in Network.GMail.Simple | |
| ToMailBody Text Source # | |
Defined in Network.GMail.Simple | |
Exceptions
data GMailException Source #
Exceptions thrown by functions in this library.
Constructors
| 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
| Exception GMailException Source # | |
Defined in Network.GMail.Simple Methods toException :: GMailException -> SomeException # | |
| Show GMailException Source # | |
Defined in Network.GMail.Simple Methods showsPrec :: Int -> GMailException -> ShowS # show :: GMailException -> String # showList :: [GMailException] -> ShowS # | |