Sending Mailgun Inline Images in HTML using Python Requests library

后端 未结 2 1117
日久生厌
日久生厌 2020-12-17 00:39

I am having trouble working out how I can send multiple inline messages using the Mailgun api, from a Python app using the requests library. Currently I have (using jinja2 f

相关标签:
2条回答
  • 2020-12-17 01:10

    As of 2020, actual documentation here: https://documentation.mailgun.com/en/latest/api-sending.html#examples

    My example:

    response = requests.post(
        'https://api.mailgun.net/v3/' + YOUR_MAILGUN_DOMAIN_NAME + '/messages',
        auth=('api', YOUR_MAILGUN_API_KEY),
        files=[
            ('inline[0]', ('test1.png', open('path/filename1.png', mode='rb').read())),
            ('inline[1]', ('test2.png', open('path/filename2.png', mode='rb').read()))
        ],
        data={
            'from': 'YOUR_NAME <' + 'mailgun@' + YOUR_MAILGUN_DOMAIN_NAME + '>',
            'to': [adresat],
            'bcc': [bcc_adresat],
            'subject': 'email subject',
            'text': 'email simple text',
            'html': '''<html><body>
                <img src="cid:test1.png">
                <img src="cid:test2.png">
                </body></html>'''
        },
        timeout=5  # sec
    )
    
    0 讨论(0)
  • 2020-12-17 01:28

    Sending Inline Images is documented here.

    In the HTML, you'll reference the image like this:

    <html>Inline image here: <img src="cid:test.jpg"></html>
    

    Then, define a Multidict, to post the files to the API:

    files=MultiDict([("inline", open("files/test.jpg"))])
    

    Disclosure, I work for Mailgun. :)

    0 讨论(0)
提交回复
热议问题