简体   繁体   中英

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. In the Thunderbird email client, it looks like this: t<>, e<>, s<>, t<>, @<>, g<>, m<>, a<>, i<>, l<>, .<>, c<>, o<>, m<>, . I am using gmail to send these emails. 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 , .

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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