How to send email attachments using python httplib2 with Mailgun

2 views
Skip to first unread message

Isaac via StackOverflow

unread,
Feb 17, 2017, 6:13:05 AM2/17/17
to google-appengin...@googlegroups.com

I am using httplib2 with the Mailgun API to send an email attachment which I downloaded using the Google Drive, the email is sending but without attachments.. below is my code..

DRIVE = discovery.build('drive', 'v3', http=http_auth)

        request = DRIVE.files().export_media(fileId=file_id, mimeType='application/pdf')

        fh = io.BytesIO()
        downloader = MediaIoBaseDownload(fh, request)

        done = False
        while done is False:
            status, done = downloader.next_chunk()
            logging.info("Download %d%%." % int(status.progress() * 100))

        messages = {
            "from": sender,
            "to": recipient,
            "subject": 'Attachment Mail from Mailgun',
            "text": 'Testing',
            "attachment": fh.getvalue()
        }

         url = URL

        data = {
            "from": messages['from'],
            "to": messages['to'],
            "subject": messages['subject'],
            "text": messages['text'],
            "attachment": messages['attachment']
        }

        pl = urllib.urlencode(data)

        http = httplib2.Http()
        http.add_credentials('api', API)

        resp, content = http.request(
            url, 'POST', pl,
            headers={"Content-Type": "application/x-www-form-urlencoded"})


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/42296370/how-to-send-email-attachments-using-python-httplib2-with-mailgun

Zebs via StackOverflow

unread,
Feb 19, 2017, 12:53:08 AM2/19/17
to google-appengin...@googlegroups.com

We use the mailgun API to send emails using Appengine and reading from cloud storage, the same principles will apply to google drive:

The first thing I would suggest is looking into StringIO. It allows you to simulate working with files inside the appengine sandbox in an easier way than BytesIO, but both produce what python calls file objects that support .read() so this should work with both.

Once you have your file as a file like object, you just need to pass it correctly to the API. The following example uses the requests library as it makes it really easy to POST with files and is compatible with appengine.

Notice that in this case open(FILE_PATH_1, 'rb') is a file like object, you just need to replace that with your file object:

def send_complex_message():
    return requests.post("https://api.mailgun.net/v2/DOMAIN/messages",
          auth=("api", "key-SECRET"),
          files={
              "attachment[0]": ("FileName1.ext", open(FILE_PATH_1, 'rb')),
              "attachment[1]": ("FileName2.ext", open(FILE_PATH_2, 'rb'))
          },
          data={"from": "FROM_EMAIL",
                "to": [TO_EMAIL],
                "subject": SUBJECT,
                "html": HTML_CONTENT
          })


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/42296370/how-to-send-email-attachments-using-python-httplib2-with-mailgun/42324107#42324107
Reply all
Reply to author
Forward
0 new messages