I recently saw a python script to prevent DDOS attacks, but its email notification function has never been successful, and I have recently learned python. Open the source code and find that smtplib is called to send emails. However, it is a little complicated to call the configuration file parameters. First, I checked the usage of smtplib on the Internet. Use smtplib to send code
Example:
| The code is as follows: |
Copy code |
#! /Usr/bin/env python Import smtplib Import sys Import email. mime. text # My test mail Mail_username = '361way @ gmail.com' Mail_password = 'test' From_addr = mail_username To_addrs = ('test @ 361way.com ') # HOST & PORT HOST = 'smtp .gmail.com' PORT = 25 # Create SMTP Object Smtp = smtplib. SMTP () Print 'ing ing ...' # Show the debug log Smtp. set_debuglevel (1) # Connet Try: Print smtp. connect (HOST, PORT) Except t: Print 'connect ERROR ****' # Gmail uses ssl Smtp. starttls () # Login with username & password Try: Print 'loginning ...' Smtp. login (mail_username, mail_password) Except t: Print 'login ERROR ****' # Fill content with MIMEText's object Msg = email. mime. text. MIMEText ('Hi, this is a test mail ') Msg ['from'] = from_addr Msg ['to'] = ';'. join (to_addrs) Msg ['subobject'] = 'Hello, today is a special day' Print msg. as_string () Smtp. sendmail (from_addr, to_addrs, msg. as_string ()) Smtp. quit () |
The code is quite simple. You only need to replace the user, password, recipient, subject, and content.
The basic commands for smtp protocol include:
HELO identifies a user to the server
MAIL initialize mail transmission MAIL from:
RCPT identifies a single email recipient. It is often behind the MAIL command and can have multiple rcpt:
After one or more RCPT commands, DATA indicates that all email recipients have been identified and DATA transmission is initialized to end.
VRFY is used to verify whether the specified user or email address exists. This command is often disabled by the server for security reasons.
EXPN verifies whether the specified mailbox list exists and expands the mailbox list.
HELP query commands supported by the server
No NOOP operation, the server should respond OK
QUIT end session
RSET resets the session and the current transmission is canceled.
Mail from specifies the sender address
Recipient address specified by RCPT