我有一个flask应用程序,我想在其中发送电子邮件以及从表单中获取的一些数据。一切正常,但问题是,当收到电子邮件时,未呈现HTML代码,而仅显示原始代码。这是我到目前为止所做的
if google_response['success']: #this line is used for a ReCaptcha response
msg = Message('Thank you for contacting me', sender='(my email address is put here as a string)', recipients=[request.form['email']])
name = request.form['name']
msg.body = render_template('email.html', name=name)
mail.send(msg)
return render_template('index.html')
else:
return render_template('index.html')
什么,我做错了吗?
答案 0 :(得分:0)
我认为这与您创建电子邮件的方式有关。您应该使用Multipart Email
来这样做。我的猜测是您正在使用HTML作为电子邮件的文本,而不是实际将其附加到电子邮件。
由于您没有向我们提供任何代码,因此,我将为您提供一个示例,说明如何生成包含HTML格式的电子邮件。
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
to_address = ''
from_address = ''
msg = MIMEMultipart('alternative')
msg['Subject'] = ''
msg['From'] = from_address
msg['To'] = to_address
text = ''
html = 'your HTML code goes here'
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('', '')
mail.sendmail(to_address, from_address, msg.as_string())
mail.quit()