0

Thanks for your kind help, i managed to follow mailgunn's latest documentation to create the function below. https://documentation.mailgun.com/en/latest/api-sending.html#examples

However, when running the script/ sending the email, i still get the error that says " UnicodeDecodeError: 'cp932' codec can't decode byte 0xe0 in position 3: illegal multibyte sequence". i googled abit and found that the problem is in the error encoding. Basically, i am drawing a plot using matplotlib/seaborn and doing a plt.savefig("TEST.jpg").

can u help me with this error?

    # function to send email out
    def Heroku_Mailgun_Send_Email(self, Email_to, Email_subject, Email_body, FileAttachment, ChartImage, Email_API_key, Email_domain):
        return requests.post(
            f"https://api.mailgun.net/v3/{Email_domain}/messages",
            auth=("api", Email_API_key),
            files=[("inline", open(ChartImage)),
                ("attachment", (FileAttachment, open(FileAttachment,"rb").read()))],
            data={"from": f"NG Ying Jian <mailgun@{Email_domain}>",
                "to": Email_to,
                "subject": Email_subject,
                "text": Email_body,
                "html": f'<html>Chart image here: <img src="cid:{ChartImage}"></html>'})

###################################

Im using python, and trying to build a function that sends out an email with a 'png' format image file in it (passed over by the argument called "ChartImage"). However, when i use this function below to send out below, the image doesnt get sent out. it says "The linked image cannot be displayed. the file may have been moved, renamed or deleted..."

my gut feeling tells me i need to insert something "inline" in the "files=" portion so that it can be called below in the , but i dont know how to do it. able to help?

    # function to send email out
    def Heroku_Mailgun_Send_Email(Email_to, Email_subject, Email_body, FileAttachment, ChartImage, Email_API_key, Email_domain):
        return requests.post(
            f"https://api.mailgun.net/v3/{Email_domain}/messages",
            auth=("api", Email_API_key),
            files=[("attachment", (FileAttachment, open(FileAttachment,'rb').read()))],
            data={
                "from": f"NG Ying Jian <mailgun@{Email_domain}>",
                "to": Email_to,
                # "cc": "baz@example.com",
                # "bcc": "bar@example.com",
                "subject": Email_subject,
                "text": Email_body,
                "html": f"<html> \
                            <head> \
                                <title>HTML img Tag</title> \
                            </head> \
                            <body> \
                                <img src={ChartImage} width='1000' height='500'>> \
                            </body> \
                        </html>",
                })
yjng
  • 1
  • 2
  • see https://stackoverflow.com/questions/920910/sending-multipart-html-emails-which-contain-embedded-images – mti2935 Aug 29 '21 at 02:40
  • Thank you for this!! I read through the link but i'm actually trying to send via mailgun API, instead of SMTP. how do i do that? https://documentation.mailgun.com/en/latest/api-sending.html#examples – yjng Aug 29 '21 at 02:57

1 Answers1

0

First, use the method described in the accepted answer at Sending Multipart html emails which contain embedded images to create a message containing the embedded image.

Then, use python's smtplib library to send the message through Mailgun's SMTP service. You can find documentation here and examples here. You'll need to specifiy Mailgun's SMTP settings, which you can find here.

mti2935
  • 11,465
  • 3
  • 29
  • 33
  • Mailgun has both API service and SMTP service. i want to send it using the API service because it seems to be easier to do so.. how can i do it?? https://www.mailgun.com/blog/difference-between-smtp-and-api/ – yjng Aug 29 '21 at 03:54
  • Looks like Mailgun has an example for sending an HTML email with an inline image through their API here: https://documentation.mailgun.com/en/latest/user_manual.html#sending-via-api – mti2935 Aug 29 '21 at 03:59
  • Thanks! im still getting another error called "UnicodeDecodeError: 'cp932' codec can't decode byte 0xe0 in position 3: illegal multibyte sequence" when i try to send the ChartImage file, which is "TEST.jpg". i did a plt.savefig("TEST.jpg") after plotting it – yjng Aug 29 '21 at 04:39