1

I am using office 365 and Flask-mail to send email to users but the format received is a winmail.dat. Is there any configuration required to send email on html format. Thanks

fouad09
  • 11
  • 1
  • 3

1 Answers1

4

For me the answer on this question worked: Flask mail security is not meeting Microsoft Outlook's security requirements?

Long story short: the message ID of the email is too long for office 365 (greater than 78 characters). The message ID is created automatically. The length depends on where your application is running. In my case, locally it was short enough, but on an aws ec2 instance it was too long.

The solution: shorten the message ID.

Here's a rough code:

from flask import Flask
from flask_mail import Mail, Message
import os

# instantiate flask app
app = Flask(__name__)

# set configuration and instantiate mail
mail_settings = {
    "MAIL_SERVER": 'smtp.office365.com',
    "MAIL_PORT": 587,
    "MAIL_USE_TLS": True,
    "MAIL_USE_SSL": False,
    "MAIL_USERNAME": os.environ['EMAIL_USER'],
    "MAIL_PASSWORD": os.environ['EMAIL_PASSWORD']
}
app.config.update(mail_settings)
mail = Mail(app)

# create message
msg = Message(subject=...,
              sender=...,
              recipients=...,
              body=...)

# change message ID
msg.msgId = msg.msgId.split('@')[0] + '@short_string'  # for instance your domain name

# send email
mail.send(msg)

With dir(msg) you find the attribute msgId. You can look at it with print(msg.msgId), which for instance returns <15697538111.34514.8222011112852912398@DESKTOP-8RIS71Z.home>. You can check the length with len(msg.msgId).

When you change the message ID, add some logic to make sure the length doesn't exceed the limit.

Raffael
  • 41
  • 3