0
我編寫了一個成功發送郵件的電子郵件模塊,但收件人將他們的電子郵件地址顯示爲逗號分隔的字符列表。在gmail網頁郵件頁面中,它看起來像這樣:t, e, s, t, @, g, m, a, i, l, ., c, o, m,
,但在每個字母之間換行。在Thunderbird電子郵件客戶端中,它看起來像這樣:t<>, e<>, s<>, t<>, @<>, g<>, m<>, a<>, i<>, l<>, .<>, c<>, o<>, m<>,
。我正在使用Gmail來發送這些電子郵件。以下是我寫的電子郵件模塊。任何人都可以幫助我弄清楚如何使電子郵件地址正常顯示給收件人?先謝謝你。Python,Gmail,發送郵件以地址列表形式顯示地址
import smtplib
import ConfigurationManager
from email import encoders
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
class EmailHandler(object):
def __init__(self, passed_values):
self._filename = passed_values.get('filename')
self._subject = passed_values.get('subject')
self._from_address = passed_values.get('from_address')
self._to_addresses = passed_values.get('to_addresses')
self._email_password = passed_values.get('email_password')
self._body = passed_values.get('body')
@property
def filename(self):
return self._filename
@filename.setter
def filename(self, value):
self._filename = value
@property
def from_address(self):
return self._from_address
@from_address.setter
def from_address(self, value):
self._from_address = value
@property
def to_addresses(self):
return self._to_addresses
@to_addresses.setter
def to_addresses(self, value):
self._to_addresses = value
@property
def body(self):
return self._body
@body.setter
def body(self, value):
self._body = value
@property
def subject(self):
return self._body
@body.setter
def body(self, value):
self._body = value
def send_email_with_attachment(self):
msg = MIMEMultipart()
msg['From'] = self._from_address
msg['To'] = ', '.join(self._to_addresses)
msg['Subject'] = self._subject
msg.attach(MIMEText(self._body, 'plain'))
attachment = open (self._filename + '.zip', "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % self._filename + '.zip')
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(self._from_address, self._email_password)
text = msg.as_string()
server.sendmail(self._from_address, self._to_addresses, text)
server.quit()
是你偶然會把'to_addresses'作爲一個字符串?它應該是一個字符串列表。如果你只想傳遞一個to_address,那麼它應該仍然是一個只有一個元素的列表,也就是'['[email protected]']' –
就是這樣。一個非常簡單的變化,因爲電子郵件地址已經在列表中開始了。我正在將它們轉換爲字符串,因爲我認爲這是必要的。非常感謝你!如果您將此作爲答案發布,我會接受它。 – OnlyDean
發表回答,謝謝! –