簡體   English   中英

如何在python電子郵件腳本中在發件人地址之前添加發件人姓名

[英]How to add sender name before sender address in python email script

這是我的代碼

# Import smtplib to provide email functions
    import smtplib

    # Import the email modules
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText

    # Define email addresses to use
    addr_to   = 'user@outlook.com'
    addr_from = 'user@aol.com'

    # Define SMTP email server details
    smtp_server = 'smtp.aol.com'
    smtp_user   = 'user@aol.com'
    smtp_pass   = 'pass'

    # Construct email
    msg = MIMEMultipart('alternative')
    msg['To'] = addr_to
    msg['From'] = addr_from
    msg['Subject'] = 'test test test!'

    # Create the body of the message (a plain-text and an HTML version).
    text = "This is a test message.\nText and html."
    html = """\

    """

    # Record the MIME types of both parts - text/plain and text/html.
    part1 = MIMEText(text, 'plain')
    part2 = MIMEText(html, 'html')

    # Attach parts into message container.
    # According to RFC 2046, the last part of a multipart message, in this case
    # the HTML message, is best and preferred.
    msg.attach(part1)
    msg.attach(part2)

    # Send the message via an SMTP server
    s = smtplib.SMTP(smtp_server)
    s.login(smtp_user,smtp_pass)
    s.sendmail(addr_from, addr_to, msg.as_string())
    s.quit()

我只希望收到的電子郵件在發件人電子郵件地址之前顯示發件人姓名,如下所示: sender_name

這取決於“友好名稱”是基本 ASCII 還是需要特殊字符。

基本示例:

msg['From'] = str(Header('Magnus Eisengrim <meisen99@gmail.com>'))

如果您需要使用非 US-ASCII 字符,則比較復雜,但隨附的文章應該會有所幫助,非常徹底: http : //blog.magiksys.net/generate-and-send-mail-with-python-tutorial

這是一個老問題 - 但是,我遇到了同樣的問題並提出了以下問題:

msg['From'] = formataddr((str(Header('Someone Somewhere', 'utf-8')), 'xxxxx@gmail.com'))

您需要from email.header import Headerfrom email.utils import formataddr

這將使收件箱中只顯示發件人姓名,而沒有<xxxxx@gmail.com>

雖然電子郵件正文將包含完整模式:

將發件人姓名和電子郵件放在一個字符串中( Sender Name <sender@server.com> )將使某些電子郵件客戶端在收件人的收件箱中相應地顯示它(與第一張圖片不同,僅顯示姓名)。

在 2020 年和 Python 3 中,你做這樣的事情:

from email.utils import formataddr
from email.message import EmailMessage
import smtplib

msg = EmailMessage()
msg['From'] = formataddr(('Example Sender Name', 'john@example.com'))
msg['To'] = formataddr(('Example Recipient Name', 'jack@example.org'))
msg.set_content('Lorem Ipsum')

with smtplib.SMTP('localhost') as s:
    s.send_message(msg)

我采用了內置示例並使用以下方法實現:

mail_body = "the email body"
mailing_list = ["user1@company.com"]
msg = MIMEText(mail_body)

me = 'John Cena <mail@company.com>'
you = mailing_list
msg['Subject'] = subject
msg['From'] = me
msg['To'] = mailing_list

# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()

我發現,如果我使用 gmail 發送電子郵件並將From標頭設置為sender name <email@gmail.com> ,則電子郵件會以From到達:

來自發件人姓名 email@gmail.com email@gmail.com。

所以我想至少對於 gmail,你應該像下面這樣設置From標頭:

msg['From'] = "sender name"

您可以使用下面提到的代碼,您只需要使用用戶名和密碼更改發送者和接收者,它就會為您工作。

import smtplib

sender = 'xyz@gmail.com'
receivers = ['abc@gmail.com']

message = """From: sender_name <xyz@gmail.com>
To: reciever_name <abc@gmail.com>
Subject: sample test mail

This is a test e-mail message.
"""

try:
   smtpObj = smtplib.SMTP('smtp_server',port)
   smtpObj.sendmail(sender, receivers, message) 
   smtpObj.login(user,password)        
   print ("Successfully sent email")
except:
   print ("Error: unable to send email")

更多詳情請訪問https://www.datadivein.com/2018/03/how-to-auto-send-mail-using-python.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM