Home  >  Article  >  Backend Development  >  Python3 implements the function of sending QQ emails (attachments)_python

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

韦小宝
韦小宝Original
2017-12-16 09:39:401482browse

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

The examples in this article are shared with everyone. Using Python3 to implement the function of sending QQ emails: Attachment, for your reference, the specific content is as follows

The email attachment can be successfully sent, but the main content of the email cannot be sent. Please find out the reason when you have time


import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from email.header import Header
my_sender = 'xxxx@qq.com'  # 发件人邮箱账号
my_pass = 'xxxxx'       # 发件人邮箱密码(当时申请smtp给的口令)
my_user = 'xxxxxxx@qq.com'   # 收件人邮箱账号,我这边发送给自己r


def mail():
  ret = True
  try:
    msg = MIMEText(open('a.txt', 'rb').read(), 'base64', 'utf-8')
    msg["Content-Type"] = 'application/octet-stream'
    msg["Content-Disposition"] = 'attachment; filename="a.txt"'

    msg['From']=formataddr(["xxxxx", my_sender]) 
     # 括号里的对应发件人邮箱昵称、发件人邮箱账号
    msg['To']=formataddr(["xxxxxx", my_user])       
     # 括号里的对应收件人邮箱昵称、收件人邮箱账号
    msg['Subject']= '邮件主题'      
     # 邮件的主题,也可以说是标题

    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 content of this article. I hope it will be helpful to everyone’s learning. I also hope that everyone will support the PHP Chinese website.

Related recommendations:

How to set excel cell font and format using xlwt in Python

Example of implementing Baidu speech recognition function in Python

Example analysis of the implementation method of Python cross-file global variables

The above is the detailed content of Python3 implements the function of sending QQ emails (attachments)_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