0

我正在尝试使用 SMTP 在 Python 中发送一封电子邮件,其中包含发件人地址、收件人地址、密件抄送地址、主题和消息。我有电子邮件发送,它甚至应该发送到密件抄送,唯一的问题是电子邮件的消息说:

To: example@gmail.com

Subject: Subject goes here

this is the email that I’m sending

当我只希望邮件本身显示邮件所属的位置,并且未设置电子邮件的主题时,因此有一个空白主题。这是我的设置方式:

def sendEmail(fromAddress, toAddress, bccAddress, appName, message):

    subject = "Subject goes here"
    BODY = string.join((
            "From: %s\r\n" % fromAddress,
            "To: %s\r\n" % toAddress,
            "Subject: %s\r\n" % subject,
            "\r\n",
            message
            ), "\r\n")

    #im using arbitrary values here, when I run it I use actual login info
    username = 'example@gmail.com'
    password = 'password'
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login(username,password)

    toList = []
    bccList = []
    toList.append(toAddress)
    bccList.append(bccAddress)
    server.sendmail(fromAddress, toList + bccList, BODY)
    server.quit()
4

1 回答 1

0

使用email包(文档)。

from email.mime.text import MIMEText

def send_mail(to, from_addr, subject, text):
    msg = MIMEText(text)
    msg['Subject'] = subject
    msg['From'] = from_addr
    msg['To'] = to
    s = smtplib.SMTP_SSL("smtp.gmail.com")
    s.login(smtp_user, smtp_pass)
    # for Python 3
    s.send_message(msg)
    # OR
    # for Python 2 (or 3, will still work)
    s.sendmail(from_addr, [to], msg.as_string())
    s.quit()
于 2015-02-27T00:16:56.843 回答