Python Send mail script

Source: Internet
Author: User
This article mainly for you to send a detailed introduction of the Mail Python script, support multiple attachments, Chinese, with a certain reference value, interested in small partners can refer to

The example of this article for everyone to share the python sent the specific code for you to refer to, the specific content as follows

#!/usr/bin/env python #-*-coding:utf-8-*-import smtplib import sys from email.mime.text import mimetext import Lineca Che import email import os ##################### # Set email service host,user,pass word,postfix mail_host= "Smtp.exmail. Qq.com "mail_user=" username "mail_pass=" password "mail_postfix=" qq.com "###################### def transfer_utf8_to_ GB2312 (file_name): F=open (file_name) S=f.read () F.close () U=s.decode ("Utf-8") S=u.encode ("gb2312") F=open (file_name,  "W"); F.write (s) def Send_mail (to_list,sub,content_file_name): me=mail_user+ "<" +mail_user+ "@" +mail_postfix+ ">" msg = email. Mimemultipart.mimemultipart () content = open (Content_file_name.encode ("Utf-8"), ' RB ') content_msg = Mimetext ( Content.read (), "plain", "Utf-8") Msg.attach (content_msg) msg[' Subject '] = Sub msg[' from '] = Me msg[' to '] = ";". Join (to_list) try:s = Smtplib.   SMTP () s.connect (mail_host) S.login (mail_user+ "@" +mail_postfix,mail_pass) s.sendmail (Me, To_list, msg.as_string ()) S. Close () return True except Exception, e:print "error:", str (e) return False def send_mail_with_attachment (to_list , sub,content_file_name,attachment_file_name): me=mail_user+ "<" +mail_user+ "@" +mail_postfix+ ">" msg = email. Mimemultipart.mimemultipart () content = open (Content_file_name.encode ("Utf-8"), ' RB ') content_msg = Mimetext ( Content.read (), "plain", "Utf-8") Msg.attach (content_msg) for tmp_attachment_file_name in Attachment_file_name.split ( ","): Contype = ' Application/octet-stream ' maintype, subtype = contype.split ('/', 1) file_data = open (tmp_attachment _file_name.encode ("Utf-8"), ' RB ') file_msg = email. Mimebase.mimebase (MainType, subtype) file_msg.set_payload (File_data.read ()) file_data.close () email. Encoders.encode_base64 (file_msg) basename = Os.path.basename (tmp_attachment_file_name) File_msg.add_header (' Content-disposition ', ' attachment ', filename = Basename.encode ("Utf-8")) Msg.attach (file_msg) msg[' Subject '] = Sub msg [' from '] = Me msg['To '] = ";". Join (to_list) try:s = Smtplib.   SMTP () s.connect (mail_host) S.login (mail_user+ "@" +mail_postfix,mail_pass) s.sendmail (Me, To_list, msg.as_string ()) S.close () return True except Exception, e:print "error:", str (e) return False def print_usage (): print "Usage: "Print"%s email_send_list (xxx@163.com,xxx@qq.com,...) Subject Content_file_name "% (Sys.argv[0]) print"%s email_send_list (xxx@163.com,xxx@qq.com,...) Subject Content_file_name Attachment_file_name (file_name1,file_name2,...) if_transform_attachment_to_gb2312 (yes or Not) "% (Sys.argv[0]) ##### #Start from here######### if __name__ = = ' __main__ ': Reload (SYS) sys.setdefaultencoding (' UTF 8 ') If Len (sys.argv) = = 6:send_list = Sys.argv[1].split (",") Subject = Unicode (sys.argv[2], "Utf-8") content_file_n ame = Unicode (sys.argv[3], "utf-8") Attachment_file_name = Unicode (sys.argv[4], "Utf-8") if (sys.argv[5] = = "Yes"): TR ansfer_utf8_to_gb2312 (Attachment_file_name.decode ("Utf-8")) elif (syS.ARGV[5] = = "not"): Pass Else:print_usage () if Send_mail_with_attachment (Send_list,subject,content_file_name,   Attachment_file_name): print "Send email success!"    Else:print "Send Email fail!" Sys.exit (1) elif len (sys.argv) = = 4:send_list = Sys.argv[1].split (",") Subject = Unicode (sys.argv[2], "Utf-8") cont Ent_file_name = Unicode (sys.argv[3], "Utf-8") if Send_mail (send_list,subject,content_file_name): print "Send email su   Ccess! "    Else:print "Send Email fail!" Sys.exit (1) else:print_usage ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.