python发送给1个人和2个密件抄送

时间:2017-07-18 12:42:43

标签: python python-2.7 email

发送邮件时遇到问题。从我从程序中获得的电子邮件,但是当我发送给用户时。他们可以看到密送电子邮件地址。我已经搜索了答案,但大多数是针对多地址而不是cc或bcc地址

所以我需要一个能够发送密件抄送的解决方案,但是用户隐藏了这个密码,而密件抄送收件人

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

1 个答案:

答案 0 :(得分:0)

替换它:

msg['To'] = to # @newuser.be
msg['Bcc'] = "tt@boss.be"
msg['cc'] = "zz@boss.be"

使用:

msg['To'] = to 
msg['cc'] = "zz@boss.be"
to = [ to, "zz@boss.be", "tt@boss.be" ]