python——smtp邮件发送
作者:故笺/gujian
链接地址:https://www.cnblogs.com/gujianjian/p/12882006.html
说明:码农不易,请尊重他人劳动成果共创和谐网络环境。本文非常欢迎转载但请备注原作出处,违者必究。
# 发送邮件功能:函数从左往右的参数分别为;email_subject:邮件主题名称,file_path:发送文件路径集合,可以为多个文件, # to_add:发送的对象,可以用"1,2,3,4,5,6"的模式分发不同的邮件对象,body:邮件发送正文:目前是固定一个文本,后期可以进行优化设置。 def send_mail(email_subject, file_paths, to_add, body='mail by Python from Dan on Windows 10'): from_add = '***********.com' cc_add = "************.com','********.com" msgroot = MIMEMultipart('related') msgroot['Subject'] = email_subject # 邮件名称 msgroot['from'] = from_add # 邮件发送人 msgroot['to'] = to_add # 邮件发送对象 msgroot['Cc'] = cc_add # 邮件抄送人 email_body = MIMEText(body, 'html', 'utf-8') # 邮件正文内容 msgroot.attach(email_body) for i in range(0, len(file_paths)): file_path = file_paths[i] file_name = file_paths[i].split('\\')[-1] att = MIMEText(open(file_path, 'rb').read(), 'base64', 'utf-8') att['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' att.add_header("Content-Disposition", "attachment", filename=("gbk", "", file_name)) #attname = 'attachment;filename =%s' % file_name # 重命名 #att['Content-DIsposition'] = attname msgroot.attach(att) server = 'smtp.263.net' s_smtp = smtplib.SMTP(server, 25) s_smtp.connect(server) s_smtp.login('****.com', '********') s_smtp.sendmail(from_add,to_add.split(',')+cc_add.split(','), str(msgroot)) s_smtp.quit() print('$sent:succesful')