Python implements the mail sending function, and python implements mail sending.

Source: Internet
Author: User
Tags email account

Python implements the mail sending function, and python implements mail sending.

The example in this article shares the code for implementing the mail sending function in python for your reference. The details are as follows:

Dependency:

The Python code is used to send emails. The smtplib and MIMEText modules are used. before implementing the code, you need to import the package:

import smtplibfrom email.mime.text import MIMEText

Use 163 to send emails. The Code is as follows:

Import smtplibfrom email. mime. text import MIMEText ''' refers to the mail sending function, which uses 163 smtp: param mail_host: email server by default, 16 mail host: smtp.163.com: param port: port number, and 163 mail's default port is 25: param username: email account xx@163.com: param passwd: Mailbox password (not the login password of the mailbox, is the authorization code of the mailbox): param recv: Mail recipient address, multiple accounts separated by commas: param title: Mail title: param content: Mail content: return: '''def send_mail (username, passwd, recv, title, content, mail_host = 'smtp .163.com ', port = 25): msg = MIMEText (content) # message content msg ['subobject'] = title # Subject msg ['from'] = username # sender account msg ['to'] = recv # receiver account list smtp = smtplib. SMTP (mail_host, port = port) # connect to the mailbox, input the email address, and port number. the smtp port number is 25 smtp. login (username, passwd) # log on to the sender's email account and password # The parameters are respectively the sender and receiver, and the third is to change the content of the sent email to the string smtp. sendmail (username, recv, msg. as_string () smtp. quit () # exit smtp print ('email send success. ') if _ name _ =' _ main _ ': email_user = 'xxxx @ 163.com' # sender account email_pwd = 'xxxxx' # sender password, authorization code maillist = 'xxxx @ qq.com 'title = 'test mail title' content = 'here is the mail content 'send_mail (email_user, email_pwd, maillist, title, content)

163 The authorization code for an email address is obtained as follows:

1. log on to the 163 mailbox and click Settings-POP3/SMTP/IMAP, as shown below:

2. Enable the SMTP service and query the SMTP host address:

3. Click the client Authorization password to reset the authorization code:

Use QQ mail to send emails. The Code is as follows:

Import smtplibfrom email. mime. text import MIMEText ''' refers to the mail sending function. By default, 163 smtp: param mail_host: email server, QQ mail host: smtp.qq.com: param port: port number, and QQ mail default port: 456: param username: email account xx@163.com: param passwd: Mailbox password (not the login password of the mailbox, is the authorization code of the mailbox): param recv: Mail recipient address, multiple accounts separated by commas: param title: Mail title: param content: Mail content: return: ''' # def send_mail (username, passwd, recv, title, content, mail_host = 'smtp .qq.com ', port = 456): msg = MIMEText (content) # message content msg ['subobject'] = title # Subject msg ['from'] = username # sender account msg ['to'] = recv # receiver account list smtp = smtplib. SMTP_SSL (mail_host, port = port) # connect to the mailbox, input the email address, and port number. the smtp port number is 25 smtp. login (username, passwd) # log on to the sender's email account and password # The parameters are respectively the sender and receiver, and the third is to change the content of the sent email to the string smtp. sendmail (username, recv, msg. as_string () smtp. quit () # exit smtp print ('email send success. ') if _ name _ =' _ main _ ': email_user = 'xxx @ qq.com' # sender account email_pwd = 'woshinige123 '# sender password, authorization code maillist = 'xxxx @ qq.com 'title = 'test mail title' content = 'here is the mail content 'send_mail (email_user, email_pwd, maillist, title, content)

Multiple recipients with attachments:

# Import smtplib # from email. mime. text import MIMEText # ''' # function for sending emails. By default, 163 smtp #: param mail_host: email server and QQ mail host: smtp.163.com #: param port: port number are used, qq mail default port is: 25 #: param username: Mailbox account xx@163.com #: param passwd: Mailbox password (not the mailbox login password, is the mailbox authorization code) #: param recv: email Recipient address, separated by commas #: param title: Mail title #: param content: Mail content #: return: # ''' import smtplibfrom email. mime. text import MIMETextfrom email. mime. multipart import MIMEMultipart # email server address email_host = 'smtp .163.com '# sender email_user = 'xxxx @ 163.com' # authorization code email_pwd = 'woshinige123 '# multiple recipients, use semicolons to separate maillist = 'xxxx @ qq.com; aaaa@qq.com '# To separate multiple recipients, with; as the standard, the returned result is list ['xxxx @ qq.com ', 'aaaa @ qq.com '] email_info = maillist. split (';') # construct the email Content Object new_msg = MIMEMultipart () # new_msg.attach (MIMEText ('test email ..... ') # Subject new_msg ['subobject'] = 'test address' # sender new_msg ['from'] = email_user # email receiver, which is used by accounts of multiple recipients, connection. The input type is strnew_msg ['to'] = ', '.join(email_info1_open a.txt To read text. att = MIMEText(open('a.txt '). read () att ["Content-Type"] = 'application/octet-stream' # Write the attachment to be sent in a fixed format, filename specifies the attachment name att ["Content-Disposition"] = 'attachment; filename = "hahaha.txt" 'new _ msg. attach (att) # connect to the mailbox and input the email address and port number. the smtp port number is 25 smtp = smtplib. SMTP (email_host, port = 25) # The sender's email account and password. log on to smtp first. login (email_user, email_pwd) smtp. sendmail (email_user, email_info, new_msg.as_string () smtp. quit () print ('email send success. ')

Use semicolons (;) TO separate accounts for multiple recipients and use smtp. sendmail (the input recipients are of the list type). new_msg ['to'] = recv, and the receipt type is str.

To send emails, you can send attachments to multiple users:

Import smtplibfrom email. mime. text import MIMETextfrom email. mime. multipart import MIMEMultipartclass SendMail (object): def _ init _ (self, username, passwd, recv, title, content, file = None, email_host = 'smtp .163.com ', port = 25): self. username = username self. passwd = passwd self. recv = recv self. title = title self. content = content self. file = file self. email_host = email_host self. port = port def se Nd_mail (self): msg = MIMEMultipart () # if self. file: # att = MIMEText (open (self. file, encoding = 'utf-8 '). read () att ["Content-Type"] = 'application/octet-stream' att ["Content-Disposition"] = 'attachment; filename = "% s" '% self. file msg. attach (att) msg. attach (MIMEText (self. content) # msg ['subobject'] = self. title # subject msg ['from'] = self. username # sender Account # Separate multiple accounts 'aaa. @ qq.com; bbbb@163.com 'Split with semicolons and split into list self. recv = self. recv. split (';') if isinstance (self. recv, list): msg ['to'] = ','. join (self. recv) else: msg ['to'] = self. recv # recipient account list if self. username. endswith ('qq. com '): # If the sender is QQ mail self. smtp = smtplib. SMTP_SSL (self. email_host, port = self. port) else: self. smtp = smtplib. SMTP (self. email_host, port = self. port) # self. smtp. login (self. username, self. passwd) try: self. smtp. sen Dmail (self. username, self. recv, msg. as_string () failed t Exception as e: print ('error .. ', E) else: print (' sent successfully! ') Self. smtp. quit () if _ name _ = '_ main _': m = SendMail (username = 'zzzzz @ 163.com ', passwd='xxxxxxx', file='a.txt ', recv = 'xxxxxx @ qq.com; xxxxxx@qq.com ', title = 'Multiple recipients', content = 'hahaha Haha ', email_host = 'smtp .163.com', port = 25) m. send_ma

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.