In a Linux system, messages can be sent with the mail command, but when sent with an attachment, the attachment is always reported as too large an error. Without knowing how to configure Linux mail, I chose Python for sending messages with Attachments.
Import Smtplib
From Email.mime.multipart import Mimemultipart
From Email.mime.text import Mimetext
From email.mime.application import mimeapplication
mail_host= "smtp.exmail.qq.com" #设置服务器
mail_user= "#填写用户名" #用户名
mail_pass= "#用户名对应的密码" #口令
mail_postfix= "#发件箱的后缀" #发件箱的后缀
#定义发送邮件的收件人, message headers, message contents, attachments
def Send_mail (to_list,sub,content,files):
msg = Mimemultipart (_subtype= ' plain ', _charset= ' utf-8 ')
msg[' Subject ' = Sub
msg[' from '] = Mail_user
msg[' to '] = ";". Join (to_list)
Part=mimetext (content,_subtype= ' plain ', _charset= ' utf-8 ')
Msg.attach (part)
For file_name in Files:
Part = mimeapplication (open (file_name, ' RB '). read ())
Part.add_header (' content-disposition ', ' attachment ', Filename=file_name)
Msg.attach (part)
Server = Smtplib. SMTP ()
Server.connect (mail_host)
Server.login (mail_user,mail_pass)
Server.sendmail (mail_user, to_list, msg.as_string ())
Server.close ()
Return True
mailto_list=[' recipient 1', ' recipient 2']
files=[' file_name ',......]
Send_mail (mailto_list, "mail header", "message body", files)
This article is from the "three countries cold jokes" blog, Please be sure to keep this source http://myhwj.blog.51cto.com/9763975/1856600
The ability to send mail with attachments with Python