I send emails to users by email, and I include both HTML and plain text versions. In other iphone email clients, I tried the default HTML rendering, however the iphone email application decides to always render in plain text. Other emails, such as newsletters, correctly display in HTML by default in my iphone email application, so it makes me think that the problem is with the way I send the email.
Having studied this problem, I decided to remove part of the plaintext and send some letters. After that, the mail application did display HTML by default. The problem is that I need to include part of the plaintext in my letters, so removing this option is not an option.
To send emails, I use the python 3.5 email and smtplib libraries . Here are the relevant code snippets:
class GmailMailer(MailSender):
def send_email(self):
msg = self.prepare_email()
with smtplib.SMTP(m_config['GMAIL_SMTP_ADDRESS'],
m_config['GMAIL_SMTP_PORT']) as smtpserver:
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(m_config['GMAIL_USER_ADDRESS'], m_config['GMAIL_USER_PASSWORD'])
smtpserver.send_message(msg, self.from_addr, self.to_addr)
print('Email sent to: {0}, with address: {1}'.format(self.to_addr, self.from_addr))
return msg.as_string()
prepare_email is defined in the parent class and defined as:
def prepare_email(self):
msg = MIMEMultipart('alternative')
msg['Subject'] = self.subject
msg['From'] = self.from_addr
msg['To'] = self.to_addr
html_template = template_env.get_template('{0}.html'.format(self.template_name))
text_template = template_env.get_template('{0}_text.html'.format(self.template_name))
html_msg = html_template.render(self.context)
text_msg = text_template.render(self.context)
msg.attach(MIMEText(html_msg, 'html'))
msg.attach(MIMEText(text_msg, 'text'))
return msg
Jinja2 , . Gmail, Spark, , /html Jinja2.