我写了一个成功发送电子邮件的电子邮件模块,但收件人看到他们的电子邮件地址显示为以逗号分隔的字符列表。在gmail webmail页面中,它看起来像这样: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发送这些电子邮件。下面是我写的电子邮件模块。任何人都可以帮我弄清楚如何使电子邮件地址正常显示给收件人?提前谢谢。
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()
答案 0 :(得分:2)
如果您将to_addresses
作为字符串传递,则它会将字符串的每个字符与,
连接起来。
在代码中,您将其视为列表,因此即使您只传递一个电子邮件地址,它也必须是列表中的单个字符串元素,如:
['test@example.com']