我是一個Python初中,所以記住這一點。在Python腳本中,我需要設置與發件人地址不同的返回路徑地址。 (我使用Gmail作爲SMTP服務器。)可靠迫使返回路徑與Python
我已經做了很多關於這個問題的搜索,發現大量的「答案」,但沒有解決方案。我試過這個鏈接Setting Return-Path with Python sendmail for a MIME message,但它根本不適合我。我可以更改電子郵件收件人看到的「收件人:」地址,但當他們單擊「回覆」時,它會再次回到發送電子郵件地址。
這是我想寫的功能。它工作得很好,除了我需要強制一個不同的返回路徑。
#!/usr/bin/python
import smtplib
import os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import sap_mailserverdata as sf
def send_mail(sent_to, subject, body_text, sent_from_addr='', sent_from_name='', files=[], cc=[], bcc=[]):
"""Send emails with or without attachments."""
assert type(sent_to)==list
assert type(files)==list
assert type(cc)==list
assert type(bcc)==list
message = MIMEMultipart()
message['From'] = sent_from_addr
message['To'] = COMMASPACE.join(sent_to)
message['Date'] = formatdate(localtime=True)
message['Subject'] = subject
message['Cc'] = COMMASPACE.join(cc)
message.preamble = 'You need a MIME enabled mail reader to see this message.\n'
message.attach(MIMEText(body_text, 'html'))
for f in files:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(f, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
message.attach(part)
addresses = []
for x in sent_to:
addresses.append(x)
for x in cc:
addresses.append(x)
for x in bcc:
addresses.append(x)
mail_server = smtplib.SMTP(sf.server, sf.server_port)
mail_server.ehlo()
mail_server.set_debuglevel(1)
mail_server.starttls()
mail_server.login(sf.username, sf.password)
mail_server.sendmail(sent_from_addr, addresses, message.as_string())
mail_server.quit()
我錯過了這個函數能夠可靠地指定一個不同的replyto返回路徑?
太好了。這聽起來像一個哨子。似乎我需要閱讀電子郵件。 – Thinkwell 2010-11-28 02:04:48