Example of sending mail using SMTP in Python

Source: Internet
Author: User

One, SMTP sends mail

Here the Python script is to log 126 of SMTP to send mail to the QQ mailbox.
QQ mailbox use is encrypted stmp, need to encrypt version of children's shoes please pay attention to the subsequent updates.
TIPS: My local environment is a MAC system, the Windows environment needs to modify the corresponding character encoding.

#!/usr/bin/env python # -*- coding: utf-8 -*-import smtplib   Import  email.encoders From email.mime.text import mimetext from email.mime.multipart  Import mimemultipart from email.mime.base import mimebase Import getpass # Build  a new attach instance Msg = mimemultipart () # mail configuration Mail_
subject =  ' python test mail ' mail_content =  ' send with attachments ' recipient_list = [' XXX@126.com ',  ' XXX@qq.com ' smtp_server =  ' smtp.126.com '     Username = raw_input (' please input your username: ')    password =  getpass.getpass ("please input your password: ")     mail_postfix =   "126.com"   # mail attachment Def attach (file_path, file_name, type,  Postfix):     with open (file_path +  "/"  + file_name,  ' RB ')  as f:         #  set the MIME and file name of the attachment, this is the PNG type:          mime = mimebase (type, postfix, filename = file_name)          #  Add the necessary header information:          Mime.add_header (' content-disposition ',  ' attachment ',  filename = file_name)          mime.add_header (' Content-id ',  ' <0> ')          mime.add_header (' X-attachment-id ',  ' 0 ')          #  read the contents of the attachment:         mime.set_payload (F.read ())          #  Base64 Code:          Email.encoders.encode_base64 (MIME)         #  Add to Mimemultipart:      
   msg.attach (MIME)    def send_mail (recipient, title, content):      # mail info     author =  "%s<%s@%s>"  % (username, username, mail_postfix)     msg[' Subject '] = title        msg[' from '] = author       msg[' to ' ] =  ";". Join (recipient)     # send attachment     msg.attach (MIMEText ( content,  ' plain ',  ' utf-8 ')     attach ('/users/xxx/pictures/ Com.tencent.ScreenCapture ', ' qq20150827-1.png ',  ' image ',  ' png ')     attach ('/users /xxx/work/python ', ' test01.py ',  ' txt ',  ' py ')     attach ('/users/xxx/work/python ', ' Test.zip ', &NBSP; ' Zip ',  ' zip ')     try:            Server = smtplib. SMTP ()         server.set_debuglevel (1)            server.connect (smtp_server)             server.login (Username, password)             Server.sendmail (Author, recipient, msg.as_string ())             server.quit ()            return True        except exception, e:          Print str (e)         return false if __name__ ==   ' __main__ ':      if send_mail (recipient_list, mail_subject,&nbsP mail_content):          print  "sent successfully"         else:          print  "Sent  failure "

Two, SMTP (SSL) Send mail


Here need to pay attention to the use of QQ mailbox (SMTL over SSL), you need to first in its web client background to open the Smtp/pop service, and set up QQ mailbox independent password as SMTP login password, so in the use of MUA will not be reported authentication Failed's mistake.
QQ Mailbox POP3 Port: 995 SMTP Port: 587
Password use QQ mailbox Independent password

#!/usr/bin/env python # -*- coding: utf-8 -*-import smtplib   Import  email.encoders From email.mime.text import mimetext from email.mime.multipart  Import mimemultipart from email.mime.base import mimebase Import getpass # Build  a new attach instance Msg = mimemultipart () # mail configuration Mail_ subject =  ' python test mail ' mail_content =  ' send with pic  Attachment ' recipient_list = [' XXX@126.com ',  ' XXX@qq.com '] smtp_server =  "smtp.qq.com"    smtp_port =  "587" username = raw_input (' please input your  Username: ')    password = getpass.getpass ("please input your password: ") )     mail_postfix =  "qq.com"   # mail attachment Def attach (file_path ,  file_name,  type, postfix):     with open (file_path +  "/"  + file_
name,  ' RB ')  as f:         #  set the MIME and file name of the attachment, here is the PNG type:         mime = mimebase (type, postfix, filename  = file_name)         #  Add the necessary header information:          mime.add_header (' content-disposition ',  ' attachment ',  filename =  file_name)         mime.add_header (' Content-ID ',  ' <0> ') )         mime.add_header (' X-attachment-id ',  ' 0 ')          #  read the contents of the attachment:         mime.set_ Payload (F.read ())         #  Base64 code:         &Nbsp;email.encoders.encode_base64 (MIME)         #  add to Mimemultipart :         msg.attach (MIME)    Def send_mail (recipient,  title, content):      # mail info     author  =  "%s<%s@%s>"  % (username, username, mail_postfix)     msg['
Subject '] = title       msg[' from '] = author       msg[' to '] =  '; Join (recipient)     # send attachment     msg.attach (MIMEText ( content,  ' plain ',  ' utf-8 ')     attach ('/users/xxx/pictures/ Com.tencent.ScreenCapture ', ' qq20150827-1.png ',  ' image ',  ' png ')     attach ('/users /xxx/work/python ', ' test01.py ',  ' txt ',  ' py ')     attach ('/USErs/xxx/work/python ', ' test.zip ',  ' zip ',  ' zip ')     try:            server = smtplib. SMTP (Smtp_server, smtp_port)         server.starttls ()          server.set_debuglevel (1)              server.login (Username, password)             server.sendmail (Author, recipient, msg.as_string ())             server.quit ()            return true        except exception, e:          print str (e)         return false if __name__  ==  ' __main__ ':      if&nbSp;send_mail (recipient_list, mail_subject, mail_content):           print  "sent successfully"        else:           print  "Sent failure"

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.