Python Send mail script

Source: Internet
Author: User
Tags python script

First, the script is suitable for e-mail testing in Linux, only need to fill in the sending account and password and sender can, and then use python./filename.py (in the current directory). If an error is sent, the error details are thrown out.

#!/usr/bin/env python#-*-coding:utf-8-*-__author__='Apollo'Import TimeImportSmtplib fromEmail.mime.textImportMimetext_user=""        #Send Account_pwd =""        #account Password_to =""        #Sending Persondefsend_email (content): Text='[%s] Reporting:'% (Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ())) Try: Msg=mimetext (content) msg["Subject"] =text msg[" from"]    =_user msg[" to"]      =_to#s = smtplib. SMTP ("smtp.dc.mydorma.com", timeout=30) # Use port 25th (normal mail sending)s = smtplib. Smtp_ssl (host='smtp.qq.com', port=465)#Use port No. 465 (SSL encrypted send)S.set_debuglevel (1) S.login (_user, _pwd) s.sendmail (_user, _to, msg.as_string ()) S.quit ()except(Smtplib. Smtpauthenticationerror, Smtplib. Smtpconnecterror, Smtplib. Smtpdataerror, Smtplib. Smtpexception, Smtplib. Smtpheloerror, Smtplib. Smtprecipientsrefused, Smtplib. Smtpresponseexception, Smtplib. Smtpsenderrefused, Smtplib. smtpserverdisconnected) as E:Print 'Warning:%s was caught and trying to send email.\ncontent:%s\n'% (E.__class__.__name__, E.message)if __name__=='__main__': Send_email ("Prepare to work:")#Message Content

Second, the script is suitable to use other languages (such as PHP) external execution of the Python script to actually send e-mail, you need to fill in the sending account and password, and other parameters from the external, such as PHP call:

EXEC ("/data/programdir/filename.py $to $subject $content $cc", $out, $result)

If $result = = 0 is sent successfully ,$result = = 1 fails to send.

#!/usr/bin/env python#-*-coding:utf-8-*-__author__='Apollo'Import TimeImportSmtplibImportSys#using external parameters, the Sys class library must be introduced fromEmail.mime.textImportMimetext_user="[email protected]"    #e-Shipment Account_pwd =""                #Password_to = sys.argv[1]#Sending Person_CC = Sys.argv[4]#Turn to Personif_cc.strip () = ='1': Rcpt=_toElse: Rcpt= [_to] + _cc.split (",")defsend_email (content): Text='[%s] Reporting:'% (Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ())) Try: Msg=mimetext (content) msg["Subject"] = sys.argv[2] msg[" from"]    =_user msg[" to"]      =_to msg["Cc"]      =_CC S= Smtplib. SMTP ("[email protected]", timeout=30)        #s = smtplib. Smtp_ssl (host= ' smtp.qq.com ', port=465)S.set_debuglevel (1)        #S.login (_user, _pwd) # When you don't need to be authenticated, you can block the lineS.sendmail (_USER,RCPT, msg.as_string ()) S.quit ()except(Smtplib. Smtpauthenticationerror, Smtplib. Smtpconnecterror, Smtplib. Smtpdataerror, Smtplib. Smtpexception, Smtplib. Smtpheloerror, Smtplib. Smtprecipientsrefused, Smtplib. Smtpresponseexception, Smtplib. Smtpsenderrefused, Smtplib. smtpserverdisconnected) as E:Print 'Warning:%s was caught and trying to send email.\ncontent:%s\n'% (E.__class__.__name__, E.message)if __name__=='__main__': Send_email (sys.argv[3])#Message Content

If reproduced, please specify the source:http://www.cnblogs.com/chrdai/p/7791693.html

Python Send mail script

Related Article

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.