python抄送邮件
时间: 2023-11-16 09:01:38 浏览: 164
可以使用Python的email库来抄送邮件。具体步骤如下:
1.导入email库中的相关模块:from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication from email.header import Header
2.构造邮件内容:msg = MIMEMultipart() msg['Subject'] = Header('邮件主题', 'utf-8') msg['From'] = from_mail msg['To'] = to_mail msg['Cc'] = cc_mail body = MIMEText('邮件内容', 'plain', 'utf-8') msg.attach(body)
3.添加附件:attachment = MIMEApplication(open('附件路径', 'rb').read()) attachment.add_header('Content-Disposition', 'attachment', filename='附件名称') msg.attach(attachment)
4.连接SMTP服务器并发送邮件:smtp = smtplib.SMTP() smtp.connect(smtp_server, smtp_port) smtp.login(username, password) smtp.sendmail(from_mail, [to_mail, cc_mail], msg.as_string()) smtp.quit()
注意:在发送邮件之前需要先连接SMTP服务器并进行登录验证。
阅读全文