A small script that uses Python to implement an automatic mass or single message! (SMTP protocol)

Source: Internet
Author: User
Tags ssl connection

Python3, use SMTP implementation to automatically send mail!

First of all, this is accidental from other people's blog found, feel more interesting, on their own to try some, in this do a bit more detailed finishing and a little small optimization! Here is the original blog's link address!

Https://www.cnblogs.com/Python1234/p/9118363.html

Steps:

1. Send QQ mail, first must know the QQ mailbox SMTP server!

      

    2. Before sending mail, you must open the SMTP service of QQ mailbox

Set path: Settings--account--open service--Save changes

    

    3.Python SMTP support has smtplib and email two modules, email is responsible for the construction of mail, smtplib responsible for sending mail ;

      

# import Smtplib module, smtplib responsible for sending mail
Import Smtplib
# import Email module, email is responsible for structuring the mail
From Email.mime.textImport Mimetext

# Create a class
ClassSendemil:
Global Send_user
Global Email_host
Global password
# Password for the corresponding 16-bit authorization code, the 16-digit authorization code received when the service was opened
Password =' Bkdzegokffjmhbei '
# Host address of email
Email_host =' Smtp.qq.com '
# who sent it, the sender
Send_user =' [Email protected] '

DefSend_email (Self, user_list, Sub, content):
user ="Shape" +"<" + Send_user +">"
# Constructs a Mimetext object, the first parameter is the message body, the second parameter is the MIME icon type,
# Plain represents plain text, and the final mime is ' Text/plain '; the third parameter uses encoding utf-8 to ensure multi-language compatibility
message = Mimetext (content,_subtype=' Plain ',_charset=' Utf-8 ')
# Sent messages need to add header information, including sender, recipient, message subject, and other information;
# Message Subject
message[' Subject '] = Sub
# Send by
message[' from '] = user
# recipient
message[' to '] =";". Join (User_list)
# secure mail, send over SSL, create SSL secure connection
Server = Smtplib. Smtp_ssl ()
# Enable SSL connection, port is typically 465
Server.connect (email_host,465)
# login () Login SMTP server
Server.login (Send_user, password)
# sendmail () e-mail, can be sent to more than one person, here with the list, the message body is STR,
# as_string () turns the Mimetext object into str
Server.sendmail (user, user_list, Message.as_string ())
# Close the SMTP server connection
Server.close ()

# Run under the current script
if __name__ = = ' __main__ ':
# Instantiate class
send = Sendemil ()
# the object to be sent, can be multiple
user_list = [" [email protected] ']
# Message header
sub = test message
# message content
content = "ha ha ha haha '
# send
Send.send_email (User_list sub print ( "sent successfully! ")

A small script that uses Python to implement an automatic mass or single message! (SMTP protocol)

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.