我正在尝试通过Python脚本发送几封电子邮件,并且效果很好,但是当我给它提供要继续发送的地址列表时,邮件会被完全打乱(所有参数,包括标头和html标签,进入邮件正文,完全不格式化)。为什么会这样?
这是政府邮件主机,它使用Postfix作为SMTP。邮件正文包含托管在自己的服务器公共链接中的图像。有可点击的链接。关于此问题的许多其他问题都与PHP有关。但是我感觉这与Python的性质有关,在运行时有一些错误。
build_message函数:
def build_message(address):
mail = EmailMessage()
mail['From'] = MY_EMAIL
mail['To'] = address
mail['Subject'] = "Pesquisa de Sondagem Meios de Hospedagem - 2ª Etapa"
mail.add_header('Disposition-Notification-To', MY_EMAIL)
# mail.add_header('Return-Receipt-To', MY_EMAIL)
mail.set_content(read_content(content_path))
mail.add_alternative("""\
<html>
<body>
<p><a href=""" + link_url + """><img src=""" + image_path + """></a></p>
<p><a href=""" + link_url + """><img src=""" + image_path2 + """></a></p>
<p> </p>
<p>Caso não consiga abrir o questionário pela imagem, clique no link abaixo:</p>
<p><a href=""" + link_url + """>""" + link_url + """</a></p>
<p> </p>
<p>Veja os resultados passados clicando
<a href=""" + link_url2 + """>aqui</a>.</p>
<p>Augusto Ferronato Lima</p>
<p><strong>Gerência Técnica de Planejamento</strong></p>
<p><a href="mailto:""" + MY_EMAIL + """>""" + MY_EMAIL + """</a></p>
<p>(48) 3665-7436</p>
<p><a href="www.turismo.sc.gov.br">www.turismo.sc.gov.br</a></p>
<p><img src=""" + signature_url + """ /></p>
</body>
</html>
""", subtype='html')
return mail
连接和发送功能:
def main(conn_host, conn_port):
contacts = read_contacts(contacts_path)
try:
print(" ### Conectando em " + conn_host + ":" + conn_port + " ### ")
s = smtplib.SMTP(host=conn_host, port=conn_port)
s.set_debuglevel(1)
s.ehlo()
s.starttls()
s.ehlo()
s.login(MY_CREDENTIAL, PASSWORD)
s.ehlo()
print(" ### Success ###")
except:
print(" ### Success in " + conn_host + ":" + conn_port + " ### ")
for contact in contacts:
wait(2)
mail = build_message(contact)
print("Building message...")
wait(5)
try:
print(" ### Sending message to: " + contact + " ### ")
s.send_message(mail)
wait(5)
print(" ### Success mailing: " + contact + " ### ")
del mail
wait(90)
except:
print(" ### Error mailing: " + contact + " ### ")
s.quit()
Screenshot: OK/Scrambled versions of the message
对我来说看起来不错,并且可以用于小型测试,但是每当我在联系人列表中添加更多地址时,邮件在发送时就会变得混乱。请帮忙。