Python program for sending emails using smtplib

Source: Internet
Author: User


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

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.