让 python 发送电子邮件看起来像来自自定义域

问题描述

我有一个从 Namecheap 转发到我的 Gmail 帐户 ([email protected]) 的自定义电子邮件地址 ([email protected])

如果我转到我的 Gmail 帐户,我可以点击下拉列表来选择从哪个电子邮件地址发送邮件

是否可以使用 python smtplib 更改要发送的地址

def send_email(filename):
subject = "subject"
body = "sometext"
sender_email = "[email protected]"
receiver_email = "[email protected]"
password = ""

# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message["Bcc"] = receiver_email  # Recommended for mass emails

# Add body to email
message.attach(MIMEText(body,"plain"))

# Open CSV file in binary mode
with open(filename,"rb") as attachment:
    # Add file as application/octet-stream
    # Email client can usually download this automatically as attachment
    part = MIMEBase("application","octet-stream")
    part.set_payload(attachment.read())

# Encode file in ASCII characters to send by email    
encoders.encode_base64(part)

# Add header as key/value pair to attachment part
part.add_header(
    "Content-disposition",f"attachment; filename= {filename}",)

# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()

# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com",465,context=context) as server:
    server.login(sender_email,password)
    server.sendmail(sender_email,receiver_email,text)

Email forwarded domain email

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)