gpt4 book ai didi

python - 如何使用 Mimemultipart 在 Python 中发送邮件

转载 作者:太空宇宙 更新时间:2023-11-04 05:38:25 25 4
gpt4 key购买 nike

我是 Python 的新手,所以在使用 python 脚本发送邮件时遇到了一些困难。我正在使用 mime 和 smtplib。由于某种原因,我无法发送并且我的邮件没有任何正文。附加代码。任何想法 ??

import smtplib
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart

hostname='hello'

sender = 'xx@xx.com'
receivers = ['vholla@xx.com']

msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = receivers
msg['Subject'] = "Upgrade started for"+hostname+"."


try:
smtpObj = smtplib.SMTP('mail.xxx.com',25)
smtpObj.sendmail(sender, receivers, msg.as_string())
print "Successfully sent email"
except:
print "Error: unable to send email"

最佳答案

我让这个选项通过 postfix 起作用:

import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP()
s.connect('localhost') # connect to the SMTP server
doc = "<html><h1>Test title</h1></html>"
while True:
msg = MIMEText( doc, _subtype='html', _charset='utf-8' )
s.sendmail('xx@xx.com', ['vholla@xx.com'], msg.as_string())
s.quit()

关于python - 如何使用 Mimemultipart 在 Python 中发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34848944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com