python发送给1个人和2个b

2024-10-03 15:34:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我发邮件有问题。从电子邮件我从一个程序,但当我发送它的用户。他们可以看到密件抄送电子邮件地址。我已经找到了答案,但大多数是多地址,而不是抄送或密件抄送地址

因此,我需要一个解决方案,将发送密件抄送,但那里隐藏的用户和密件抄送他们

def mail(self, email_user, to, subject, text, attach,attach2, email_pwd, smtp, port):
    msg = email.MIMEMultipart.MIMEMultipart()
    msg['From'] = email_user
    msg['To'] = to # @newuser.be
    msg['Bcc'] = "tt@boss.be"
    msg['cc'] = "zz@boss.be"
    msg['Subject'] = subject
    part = MIMEText(text,'html')
    #msg.attach(email.MIMEText.MIMEText(text))
    msg.attach(part)
    if attach:
        part = email.MIMEBase.MIMEBase('application', 'octet-stream')
        part.set_payload(open(attach, 'rb').read())
        email.Encoders.encode_base64(part)
        part.add_header('Content-Disposition','attachment; filename="%s"' % os.path.basename(attach))
        msg.attach(part)
    if attach2:
        part = email.MIMEBase.MIMEBase('application', 'octet-stream')
        part.set_payload(open(attach2, 'rb').read())
        email.Encoders.encode_base64(part)
        part.add_header('Content-Disposition','attachment; filename="%s"' % os.path.basename(attach2))
        msg.attach(part)
    if( port != "" ):
        mailServer = smtplib.SMTP(smtp, port)
    else:
        mailServer = smtplib.SMTP(smtp)

    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(email_user, email_pwd)
    mailServer.sendmail(email_user, to, msg.as_string())
    mailServer.close()

Tags: totextportemail地址msgbesmtp