Python - Send mail

Keywords: Python socket codec ascii

SMTP Send Mail

Send mail

import smtplib
from email.mime.text import MIMEText
from email.header import Header

# Configuring Mailbox Server
smtpserver = "smtp.163.com"

# User/password
user = "admin@163.com"
password = "123456"

# Sender mailbox
sender = "admin@163.com"

# Receiver mailbox
receiver = "123456@163.com"

# Mail theme
subject = "Python email test"

msg = MIMEText('<html><h1>Hello!</h1></html>', "html", "utf-8")
msg["Subject"] = Header(subject, "utf-8")

if __name__ == '__main__':
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver,25)
    smtp.login(user,password)
    smtp.sendmail(sender,receiver,msg.as_string())
    smtp.quit()

Running code error reporting:

Traceback (most recent call last):
  File "C:/Users/LGY/PycharmProjects/t1/python-mail.py", line 25, in <module>
    smtp = smtplib.SMTP()
  File "D:\Python_tools\Python36\lib\smtplib.py", line 261, in __init__
    fqdn = socket.getfqdn()
  File "D:\Python_tools\Python36\lib\socket.py", line 674, in getfqdn
    hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd3 in position 2: invalid continuation byte

Solution: Modify the code in the socket package imported by SMTP

# Original code
hostname, aliases, ipaddrs = gethostbyaddr(name)
# New code
hostname, aliases, ipaddrs = gethostbyaddr(name.encode("ascii","ignore"))

Send multi-person mail

import smtplib
from email.mime.text import MIMEText
from email.header import Header

# Configuring Mailbox Server
smtpserver = "smtp.163.com"

# User/password
user = "admin@163.com"
password = "123456"

# Sender mailbox
sender = "admin@163.com"

# Receiver mailbox
receiver = ["123456@163.com","234567@qq.com"]

# Mail theme
subject = "Python-email2"

msg = MIMEText('<html><h1>Hello!</h1></html>', "html", "utf-8")
msg["Subject"] = Header(subject, "utf-8")


# More than one person receives email and displays the name of the following account directly
msg['From'] = "admin@163.com"
msg['To'] = "123456@163.com;234567@qq.com"


if __name__ == '__main__':
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver, 25)
    smtp.login(user, password)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()

Attention should be paid to adding msg ["From"] and msg ["To"], and the same as the recipient's mailbox (receiver), otherwise the following error may be reported:

smtplib.SMTPDataError: (554, b'DT:SPM 163 smtp3,G9xpCgAnchCrbkFdS_YZAA--.189S2 1564569259,please see http://mail.163.com/help/help_spam_16.htm?ip=157.122.54.188&hostid=smtp3&time=1564569259')

Copy person

Mainly add pythonmsg["cc"]="234567@qq.com"

  import smtplib
  from email.mime.text import MIMEText
  from email.header import Header

  # Configuring Mailbox Server
  smtpserver = "smtp.163.com"

  # User/password
  user = "admin@163.com"
  password = "123456"

  # Sender mailbox
  sender = "admin@163.com"

  # Receiver mailbox
  receiver = ["123456@163.com","234567@qq.com"]

  # Mail theme
  subject = "Python-email2"

  msg = MIMEText('<html><h1>Hello!</h1></html>', "html", "utf-8")
  msg["Subject"] = Header(subject, "utf-8")


  #More than one person receives mail, one recipient, and one copy.
  msg['From'] = "admin@163.com"
  msg['To'] = "123456@163.com"
  msg["cc"]="234567@qq.com"

  if __name__ == '__main__':
      smtp = smtplib.SMTP()
      smtp.connect(smtpserver, 25)
      smtp.login(user, password)
      smtp.sendmail(sender, receiver, msg.as_string())
      smtp.quit()

Add nearby mail, mainly for test reports generated in conjunction with Beautiful Report

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header

# Configuring Mailbox Server
smtpserver = "smtp.163.com"

# User/password
user = "admin@163.com"
password = "123456"

# Sender mailbox
sender = "admin@163.com"

# Receiver mailbox
receiver = ["123456@163.com","234567@qq.com"]

# Mail theme
subject = "Python-email3"

msg = MIMEMultipart()
# More than one person receives email and displays the name of the following account directly
msg['From'] = "admin@163.com"
msg['To'] = "123456@163.com"
msg["cc"]="234567@qq.com"
msg["Subject"] = Header(subject, "utf-8")
# Add text
msg.attach(MIMEText('<html><h1>Hello!</h1></html>', "html", "utf-8"))

# Add near
sendFile = open("./Test report.html", 'rb').read()
att = MIMEText(sendFile, "base64", "utf-8")
att.add_header("Content-Type", "application/octet-stream")
att.add_header("Content-Disposition", "attachment", filename="Test report.html")
msg.attach(att)

if __name__ == '__main__':
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver, 25)
    smtp.login(user, password)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()

Extension - Send up-to-date test reports

# Using os.path.getmtime in python, you can see the time when the file was created, use this time to judge and send the latest report.
import os
print(os.path.getmtime("./report/Test report.html"))

  • Realization
import os
# Remove the list of file names in the folder
list_filename = os.listdir("./report")
print(list_filename)
list_filename.sort(key=lambda x:os.path.getmtime("./report/"+x),reverse=True)
print("The latest document is entitled:",list_filename[0])

Posted by ESCForums.com on Sat, 05 Oct 2019 03:39:22 -0700