包含内容
- 发送/抄送
- 发送文本信息
- 发送附件
代码详情
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
smtp = smtplib.SMTP()
smtp.connect("smtp.exmail的地址", port=25)
smtp.login(user="发送邮件的邮箱", password="授权码")
cc_mail = ["抄送的邮件地址"]
self.receive = ["接收邮件的地址"]
message = MIMEMultipart()
message['From'] = Header("发送人昵称", 'utf-8')
message['To'] = Header("jack", 'utf-8')
message['Subject'] = Header('邮件名', 'utf-8')
message['Cc'] = ','.join(cc_mail)
msg_context = MIMEText('邮件包含异常机构的信息', 'plain', 'utf-8')
message.attach(msg_context)
file1 = MIMEText(open('本地文件的文件名.xlsx', 'rb').read(), 'base64', 'utf-8')
file1.add_header("Content-Disposition", "attachment", filename="发送的时新显示的文件名.xlsx")
message.attach(file1)
smtp.sendmail(from_addr="发送邮件的地址", to_addrs= receive + cc_mail,
msg=self.message.as_string())
smtp.quit()