简体   繁体   English

Python,Gmail,已发送电子邮件,将地址显示为字符列表

[英]Python, Gmail, Sent Emails Displaying Addresses as Lists of Characters

I wrote an email module that successfully sends emails out, but the recipients see their email addresses displayed as comma-separated lists of characters. 我编写了一个电子邮件模块,可以成功发送电子邮件,但是收件人看到的电子邮件地址显示为逗号分隔的字符列表。 In the gmail webmail page, it looks like this: t, e, s, t, @, g, m, a, i, l, ., c, o, m, , but with a new line between each letter. 在gmail网络邮件页面中,它看起来像这样: t, e, s, t, @, g, m, a, i, l, ., c, o, m, 、,但每个字母之间都有新行。 In the Thunderbird email client, it looks like this: 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<>, I am using gmail to send these emails. 我正在使用gmail发送这些电子邮件。 Below is the email module I wrote. 以下是我编写的电子邮件模块。 Can anyone help me figure out how to make the email addresses display normally to the recipients? 谁能帮我弄清楚如何使电子邮件地址正常显示给收件人? Thank you in advance. 先感谢您。

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()

If you're passing to_addresses as a string, then it will join every character of the string with the , . 如果你传递to_addresses为一个字符串,那么它将加入与字符串的每个字符,

In the code you're treating it as a list, so even if you pass only one e-mail address, it needs to be the single string element in a list, like such: 在代码中,您将其视为列表,因此,即使您仅传递一个电子邮件地址,它也必须是列表中的单个字符串元素,例如:

['test@example.com']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM