Python sends mail
#! / usr/bin/env python #-*-coding:utf-8-*-import smtplibfrom smtplib import SMTP_SSLfrom email.mime.text import MIMETextfrom email.header import Header# defines a dictionary store to send and receive Email account password subject, content and coding related information mail_info = {"from": "w673004708@163.com", # sender "to": "673004708@qq.com", # recipient "hostname": "smtp.163.com", # 163smtp server "username": "w673004708@163.com", # email account "password": "*" # email password This password is 163 mailbox authorization code "subject": "Test sending email", # email subject "mail_text": "I just want to test whether the email can be sent", # email content "mail_encoding": "utf-8" # Encoding format} # send content msg=MIMEText (mail_info ['mail_text'], "plain") in MIME format Mail_info ['mail_encoding']) # set the message subject msg [' Subject'] = Header (mail_info ['subject']) Mail_info ['mail_encoding']) # set email sender msg ["from"] = mail_info ["from"] # set recipient msg ["to"] = mail_info ["to"] # create a smtp object smtp=SMTP_SSL () # smtp.set_debuglevel (1) try:# connect to qq smtp server smtp.connect (mail_info [' hostname']) # log in to smtp.login (mail_info ['username']) with account password Mail_info ['password']) # send email smtp.sendmail (mail_info [' from'], mail_info ['to'], msg.as_string ()) # close the mail object smtp.quit () except smtplib.SMTPAuthenticationError as e: print eelse: print "sent successfully"
Note the format of python. The above can be sent directly. The topic and content are fixed.
& &
The following one can pass parameters.
[root@bogon ~] # python mail.py Today's work Migration Server plus Code launch
Sent successfully
[root@bogon ~] #
#! / usr/bin/env python #-*-coding:utf-8-*-import smtplib import sysfrom smtplib import SMTP_SSLfrom email.mime.text import MIMETextfrom email.header import Header# defines a dictionary store to send and receive Email account password subject, content and coding related information subject = sys.argv [1] content = sys.argv [2] mail_info = {"from": "w673004708@163.com", # sender "to": "673004708@qq.com", # recipient "hostname": "smtp.163.com", # qq smtp server "username": "w673004708@163.com" # email account "password": "*", # email password This password is 163 mailbox authorization code "subject": subject, # message subject "mail_text": content,# message content "mail_encoding": "utf-8" # Encoding format} # transfer content msg=MIMEText (mail_info ['mail_text'], "plain", mail_info [' mail_encoding']) # set message subject msg ['Subject'] = Header (mail_info [' subject']) Mail_info ['mail_encoding']) # set email sender msg ["from"] = mail_info ["from"] # set recipient msg ["to"] = mail_info ["to"] # create a smtp object smtp=SMTP_SSL () # smtp.set_debuglevel (1) try:# connect to qq smtp server smtp.connect (mail_info [' hostname']) # log in to smtp.login (mail_info ['username']) with account password Mail_info ['password']) # send email smtp.sendmail (mail_info [' from'], mail_info ['to'], msg.as_string ()) # close the mail object smtp.quit () except smtplib.SMTPAuthenticationError as e: print eelse: print "sent successfully"