在python中的电子邮件的from字段中添加发件人的姓名

时间:2017-06-06 08:59:07

标签: python email smtplib

我正在尝试使用以下代码发送电子邮件。

import smtplib
from email.mime.text import MIMEText

sender = 'sender@sender.com'

def mail_me(cont, receiver):
    msg = MIMEText(cont, 'html')
    recipients = ",".join(receiver)
    msg['Subject'] = 'Test-email'
    msg['From'] = "XYZ ABC"
    msg['To'] = recipients
    # Send the message via our own SMTP server.
    try:
        s = smtplib.SMTP('localhost')
        s.sendmail(sender, receiver, msg.as_string())
        print "Successfully sent email"
    except SMTPException:
        print "Error: unable to send email"
    finally:
        s.quit()


cont = """\
   <html>
     <head></head>
     <body>
       <p>Hi!<br>
          How are you?<br>
          Here is the <a href="http://www.google.com">link</a> you wanted.
       </p>
     </body>
   </html>
   """
mail_me(cont,['xyz@xyzcom'])

我想&#34; XYZ ABC&#34;在收到电子邮件时显示为发件人的姓名,其电子邮件地址为&#39; sender@sender.com'。但是当我收到电子邮件时,我收到了&#34;来自&#34;电子邮件的字段。

[![from:    XYZ@<machine-hostname-appearing-here>
reply-to:   XYZ@<machine-hostname-appearing-here>,
ABC@<machine-hostname-appearing-here>][1]][1]

我附上了收到的电子邮件的屏幕截图。

如何根据我的需要解决这个问题。

6 个答案:

答案 0 :(得分:11)

这应该有效:

msg['From'] = "Your name <Your email>"

以下示例:

import smtplib
from email.mime.text import MIMEText

def send_email(to=['example@example.com'], f_host='example.example.com', 
f_port=587, f_user='example@example.com', f_passwd='example-pass', 
subject='default subject', message='content message'):
smtpserver = smtplib.SMTP(f_host, f_port)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(f_user, f_passwd) # from email credential
msg = MIMEText(message, 'html')
msg['Subject'] = 'My custom Subject'
msg['From'] = "Your name <Your email>"
msg['To'] = ','.join(to)
for t in to:
    smtpserver.sendmail(f_user, t, msg.as_string())  # you just need to add 
this in for loop in your code.
    smtpserver.close()
print('Mail is sent successfully!!')


cont = """\
<html>
 <head></head>
 <body>
   <p>Hi!<br>
      How are you?<br>
      Here is the <a href="http://www.google.com">link</a> you wanted.
   </p>
 </body>
</html>
"""
try:
send_email(message=cont)
except:
print('Mail could not be sent')

答案 1 :(得分:0)

这应该解决它:

替换mail_me(cont,['xyz@xyzcom'])

mail_me(cont,'xyz@xyz.com')

答案 2 :(得分:0)

该名称来自FROM标题。请参阅此答案Specify a sender when sending mail with Python (smtplib)

答案 3 :(得分:0)

刚刚使用gmx.com测试了以下代码,它运行正常。虽然,你是否获得相同的里程数是一个没有实际意义的点 我已使用gmail

替换了对我的电子邮件服务的所有引用
#!/usr/bin/python

#from smtplib import SMTP # Standard connection
from smtplib import SMTP_SSL as SMTP #SSL connection
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

sender = 'example@gmail.com'
receivers = ['example@gmail.com']


msg = MIMEMultipart()
msg['From'] = 'example@gmail.com'
msg['To'] = 'example@gmail.com'
msg['Subject'] = 'simple email via python test 1'
message = 'This is the body of the email line 1\nLine 2\nEnd'
msg.attach(MIMEText(message))

ServerConnect = False
try:
    smtp_server = SMTP('smtp.gmail.com','465')
    smtp_server.login('#name#@gmail.com', '#password#')
    ServerConnect = True
except SMTPHeloError as e:
    print "Server did not reply"
except SMTPAuthenticationError as e:
    print "Incorrect username/password combination"
except SMTPException as e:
    print "Authentication failed"

if ServerConnect == True:
    try:
        smtp_server.sendmail(sender, receivers, msg.as_string())
        print "Successfully sent email"
    except SMTPException as e:
        print "Error: unable to send email", e
    finally:
        smtp_server.close()

答案 4 :(得分:0)

空格不是电子邮件地址中的有效字符。特殊字符仅允许在外部表示中使用双引号括起来。另外,大多数SMTP服务器实际上使用标头重写来确保地址采用标准格式。由于你的实际包含一个空格,它被拆分,因为它没有用引号括起来,服务器地址会附加到它上面。

您只需将msg['From'] = "XYZ ABC"替换为

即可
msg['From'] = '"XYZ ABC"'

强制将地址包含在双引号中。

答案 5 :(得分:0)

import smtplib
from email.mime.text import MIMEText

def send_email(to=['example@example.com'], f_host='example.example.com', f_port=587, f_user='example@example.com', f_passwd='example-pass', subject='default subject', message='content message'):
    smtpserver = smtplib.SMTP(f_host, f_port)
    smtpserver.ehlo()
    smtpserver.starttls()
    smtpserver.ehlo
    smtpserver.login(f_user, f_passwd) # from email credential
    msg = MIMEText(message, 'html')
    msg['Subject'] = 'My custom Subject'
    msg['From'] = "Admin"
    msg['To'] = ','.join(to)
    for t in to:
        smtpserver.sendmail(f_user, t, msg.as_string())  # you just need to add this in for loop in your code.
        smtpserver.close()
    print('Mail is sent successfully!!')


cont = """\
   <html>
     <head></head>
     <body>
       <p>Hi!<br>
          How are you?<br>
          Here is the <a href="http://www.google.com">link</a> you wanted.
       </p>
     </body>
   </html>
   """
try:
    send_email(message=cont)
except:
    print('Mail could not be sent')

上面的方法我试图发送对我有用的邮件,即使我能够发送邮件到我的Gmail帐户(垃圾邮件文件夹)。 如果您遇到任何其他相关问题,请告诉我。