【问题标题】:Python: "subject" not shown when sending email using smtplib modulePython:使用 smtplib 模块发送电子邮件时未显示“主题”
【发布时间】:2011-11-06 02:54:17
【问题描述】:

我可以使用 smtplib 模块成功发送电子邮件。但是发送电子邮件时,发送的电子邮件中不包含主题。

import smtplib

SERVER = <localhost>

FROM = <from-address>
TO = [<to-addres>]

SUBJECT = "Hello!"

message = "Test"

TEXT = "This message was sent with Python's smtplib."
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()

我应该如何编写“server.sendmail”以在发送的电子邮件中包含主题。

如果我使用 server.sendmail(FROM, TO, message, SUBJECT),它会给出关于“smtplib.SMTPSenderRefused”的错误

【问题讨论】:

    标签: python smtplib


    【解决方案1】:

    将其作为标题附加:

    message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)
    

    然后:

    server = smtplib.SMTP(SERVER)
    server.sendmail(FROM, TO, message)
    server.quit()
    

    还可以考虑使用标准 Python 模块 email - 它对您撰写电子邮件有很大帮助。

    【讨论】:

    • 2021 年,不幸的是,这会将主题添加到邮件正文中。
    • @TheoF 不,它没有。该示例构造了一个小而微不足道但完全有效的 RFC5322 消息,仍在 2021 年。但正如答案所暗示的那样,您真的很想使用email 模块来构造输入消息(如果有的话)比这更简单。
    【解决方案2】:

    这将适用于使用新的“EmailMessage”对象的 Gmail 和 Python 3.6+:

    import smtplib
    from email.message import EmailMessage
    
    msg = EmailMessage()
    msg.set_content('This is my message')
    
    msg['Subject'] = 'Subject'
    msg['From'] = "me@gmail.com"
    msg['To'] = "you@gmail.com"
    
    # Send the message via our own SMTP server.
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    server.login("me@gmail.com", "password")
    server.send_message(msg)
    server.quit()
    

    【讨论】:

    • 这是 Python 3.6+ 的最佳答案。谢谢!
    • 端口号和身份验证详细信息取决于您要连接的服务器。常见的变体包括使用端口 587(或者,对于内部服务器,使用 25,没有 SSL)和/或在 login 之前和之后执行 ehlo - 甚至两次,之前和之后。
    【解决方案3】:

    试试这个:

    import smtplib
    from email.mime.multipart import MIMEMultipart
    msg = MIMEMultipart()
    msg['From'] = 'sender_address'
    msg['To'] = 'reciver_address'
    msg['Subject'] = 'your_subject'
    server = smtplib.SMTP('localhost')
    server.sendmail('from_addr','to_addr',msg.as_string())
    

    【讨论】:

    • 邮件正文呢?去哪儿了?
    • 我最终按照这个放入正文 docs.python.org/2/library/email-examples.html#id5
    • 在当前的python版本中,这个功能现在由email.message.MailMessage管理。
    • 现在已过时,不应用于新代码。在 Python 3.6 之前,这是您必须使用的(除此之外,您也希望您的消息也有正文)。
    【解决方案4】:

    您可能应该将您的代码修改为如下内容:

    from smtplib import SMTP as smtp
    from email.mime.text import MIMEText as text
    
    s = smtp(server)
    
    s.login(<mail-user>, <mail-pass>)
    
    m = text(message)
    
    m['Subject'] = 'Hello!'
    m['From'] = <from-address>
    m['To'] = <to-address>
    
    s.sendmail(<from-address>, <to-address>, m.as_string())
    

    显然,&lt;&gt; 变量需要是实际的字符串值,或者是有效的变量,我只是将它们作为占位符填充。发送带有主题的消息时,这对我有用。

    【讨论】:

    • 我收到以下错误:from email.mime.text import MIMEText as text ImportError: No module named mime.text
    • @nsh - 使用什么版本的 Python?我在这个特定的安装上使用 2.6.6。完全有可能它在 3.x 中的位置略有不同。
    • email 库在 3.6 中进行了大修,现在更加通用和合乎逻辑。大概回顾一下现在的examples from the email documentation.
    【解决方案5】:

    查看 smtplib 文档底部的注释:

    In general, you will want to use the email package’s features to construct an email message, which you can then convert to a string and send via sendmail(); see email: Examples.

    这是email 文档的示例部分的链接,它确实显示了带有主题行的消息的创建。 https://docs.python.org/3/library/email.examples.html

    似乎 smtplib 不直接支持主题添加,并且希望消息已经被格式化为主题等。这就是 email 模块的用武之地。

    【讨论】:

    • 就像接受的答案所展示的那样,如果您知道自己在做什么,或者完全按照示例进行操作,则可以从简单的字符串构造有效的 RFC5322 消息。除此之外,这就是要走的路。
    【解决方案6】:

    我认为您必须将其包含在消息中:

    import smtplib
    
    message = """From: From Person <from@fromdomain.com>
    To: To Person <to@todomain.com>
    MIME-Version: 1.0
    Content-type: text/html
    Subject: SMTP HTML e-mail test
    
    This is an e-mail message to be sent in HTML format
    
    <b>This is HTML message.</b>
    <h1>This is headline.</h1>
    """
    
    try:
       smtpObj = smtplib.SMTP('localhost')
       smtpObj.sendmail(sender, receivers, message)         
       print "Successfully sent email"
    except SMTPException:
       print "Error: unable to send email"
    

    代码来自:http://www.tutorialspoint.com/python/python_sending_email.htm

    【讨论】:

    • 一个观察:例如 from、to 和 subject 字段必须在变量“message”的 VERY BEGINNING 处,否则,这些字段将不会被解释为它必须是预期的。我有过只插入“主题”字段的经验,而不是在变量的开头,并且消息到达收件人的邮箱时没有主题。干杯。
    【解决方案7】:
    import smtplib
     
    # creates SMTP session 
    
    List item
    
    s = smtplib.SMTP('smtp.gmail.com', 587)
     
    # start TLS for security   
    s.starttls()
     
    # Authentication  
    s.login("login mail ID", "password")
     
     
    # message to be sent   
    SUBJECT = "Subject"   
    TEXT = "Message body"
     
    message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)
     
    # sending the mail    
    s.sendmail("from", "to", message)
     
    # terminating the session    
    s.quit()
    

    【讨论】:

    • 这似乎没有比以前的答案添加任何新信息。
    • 我喜欢这个答案,因为它只使用原始问题的 smtplib 模块
    【解决方案8】:

    如果将其包装在一个函数中,这应该作为一个模板。

    def send_email(login, password, destinations, subject, message):
        server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
    
        server.login(login, password)
        message = 'Subject: {}\n\n{}'.format(subject, message)
    
        for destination in destinations:
            print("Sending email to:", destination)
            server.sendmail(login, destinations, message)
    
        server.quit()
    

    【讨论】:

      猜你喜欢
      • 2022-07-21
      • 2019-11-01
      • 2021-01-20
      • 2023-02-11
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 1970-01-01
      相关资源
      最近更新 更多