Home >Backend Development >Python Tutorial >Python3 implements the function of sending QQ emails (text)_python

Python3 implements the function of sending QQ emails (text)_python

韦小宝
韦小宝Original
2017-12-16 11:23:411996browse

This article mainly introduces in detail Python3 to implement the function of sending QQ emails. In terms of text, it has certain reference value. Friends who are interested in Python3 can refer to it

This article shares with you the function of sending QQ emails in Python3: text, for your reference, the specific content is as follows

Note: Before use, you need to set up POP3 and IMAP services in QQ and set up a third-party authorization code

Then hit the x below and fill in the relevant information


import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

my_sender='xxxx@qq.com' # 发件人邮箱账号
my_pass = 'xxxxxxx'    # 发件人邮箱密码(当时申请smtp给的口令)
my_user='xxxxxxxx@qq.com'  # 收件人邮箱账号,我这边发送给自己r


def mail():
 ret=True
 try:
  msg=MIMEText(&#39;<邮件内容>&#39;,&#39;plain&#39;,&#39;utf-8&#39;)
  msg[&#39;From&#39;]=formataddr(["xxxxxx",my_sender]) 
  # 括号里的对应发件人邮箱昵称、发件人邮箱账号
  msg[&#39;To&#39;]=formataddr(["xxxxxxx",my_user])    
  # 括号里的对应收件人邮箱昵称、收件人邮箱账号
  msg[&#39;Subject&#39;]= &#39;邮件主题&#39;   
  # 邮件的主题,也可以说是标题

  server=smtplib.SMTP_SSL("smtp.qq.com", 465) 
  # 发件人邮箱中的SMTP服务器,端口是465
  server.login(my_sender, my_pass) 
  # 括号中对应的是发件人邮箱账号、邮箱密码
  server.sendmail(my_sender,[my_user,],msg.as_string()) 
   # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
  server.quit()# 关闭连接
 except Exception:# 如果 try 中的语句没有执行,则会执行下面的 ret=False
  ret=False
 return ret

ret=mail()
if ret:
 print("邮件发送成功")
else:
 print("邮件发送失败")


The above is the entire article Content, I hope it will be helpful to everyone's learning, and I also hope everyone will support the PHP Chinese website.

Related recommendations:

Introduction to python3’s simple factory mode

A case of Python operating excel files

Examples of implementation of permutation and combination calculation operations in Python

The above is the detailed content of Python3 implements the function of sending QQ emails (text)_python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn