snoyberg / mime-mail

Compose MIME email messages.
41 stars 43 forks source link

SES: Sending Mail From Different Regions #28

Closed creichert closed 10 years ago

creichert commented 10 years ago

I'm using the mime-mail-ses package and I need to send mail from regions other than us-east-1.

I have made a simple patch in my own fork and I wanted to get your thoughts:

diff --git a/mime-mail-ses/Network/Mail/Mime/SES.hs b/mime-mail-ses/Network/Mail/Mime/SES.hs
index 3018922..92bdf18 100644
--- a/mime-mail-ses/Network/Mail/Mime/SES.hs
+++ b/mime-mail-ses/Network/Mail/Mime/SES.hs
@@ -4,6 +4,7 @@ module Network.Mail.Mime.SES
     ( sendMailSES
     , renderSendMailSES
     , SES (..)
+    , SESRegion (..)
     ) where

 import           Control.Exception           (Exception, throwIO)
@@ -37,8 +38,14 @@ data SES = SES
     , sesTo        :: [ByteString]
     , sesAccessKey :: ByteString
     , sesSecretKey :: ByteString
+    , sesRegion    :: SESRegion
     }

+data SESRegion = USEast -- * Virginia
+               | USWest -- * Oregon
+               | EUWest -- * Ireland
+               deriving Show
+
 renderSendMailSES :: MonadIO m => Manager -> SES -> Mail -> m ()
 renderSendMailSES m ses mail = liftIO (renderMail' mail) >>= sendMailSES m ses

@@ -47,7 +54,10 @@ sendMailSES manager ses msg = liftIO $ do
     now <- getCurrentTime
     let date = S8.pack $ format now
         sig = makeSig date $ sesSecretKey ses
-    req' <- parseUrl "https://email.us-east-1.amazonaws.com"
+    req' <- parseUrl $ case sesRegion ses of
+                           USEast -> "https://email.us-east-1.amazonaws.com"
+                           USWest -> "https://email.us-west-2.amazonaws.com"
+                           EUWest -> "https://email.eu-west-1.amazonaws.com"
     let auth = S8.concat
             [ "AWS3-HTTPS AWSAccessKeyId="
             , sesAccessKey ses

It would also make sense to add a simple Text field to SES which would allows the user to manually enter the string of the region (e.g. 'sesRegion = '"us-east-1"')

Let me know if either of these solutions is acceptable and I can submit a pull request if this is something you want to support in mime-mail-ses.

Thanks!

snoyberg commented 10 years ago

Let's go for the Text version, and if desired, we could provide some values in the library for common cases (e.g., usEast1 = "us-east-1"). Note that this is a breaking API change, but it's worth doing.

creichert commented 10 years ago

Awesome, Thanks!