如何解决让 python 发送电子邮件看起来像来自自定义域
我有一个从 Namecheap 转发到我的 Gmail 帐户 (name@gmail.com) 的自定义电子邮件地址 (name@company.com)
如果我转到我的 Gmail 帐户,我可以点击下拉列表来选择从哪个电子邮件地址发送邮件。
是否可以使用 python smtplib 更改要发送的地址
def send_email(filename):
subject = "subject"
body = "sometext"
sender_email = "name@gmail.com"
receiver_email = "name@gmail.com"
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。