Python-implemented simple email sending script sharing

Source: Internet
Author: User
This article mainly introduces the simple sending email script sharing implemented by Python. This article uses the smtplib module to send emails. For more information, see monitoring and sending alerts, then I found some materials on the Internet and wrote a simple email sending script. I used the python smtplib module to share it with you:

The code is as follows:


#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
# Import smtplib and MIMEText
Import smtplib, sys
From email. mime. text import MIMEText

Def send_mail (sub, content ):
#############
# To whom, send one person here
Mailto_list = ["wangwei03@jb51.net"]
#####################
# Set the server, user name, password, and mailbox suffix
Mail_host = "mail.gyyx.cn"
Mail_user = "wangwei03@jb51.net"
Mail_pass = "123456677890"
Mail_postfix = "gyyx.cn"
######################
'''''
To_list: to whom
Sub: Topic
Content: content
Send_mail ("aaa@126.com", "sub", "content ")
'''
Me = mail_user + "<" + mail_user + "@" + mail_postfix + ">"
Msg = MIMEText (content, _ charset = 'gbk ')
Msg ['subobject'] = sub
Msg ['from'] = me
Msg ['to'] = ";". join (mailto_list)
Try:
S = smtplib. SMTP ()
S. connect (mail_host)
S. login (mail_user, mail_pass)
S. sendmail (me, mailto_list, msg. as_string ())
S. close ()
Return True
Except t Exception, e:
Print str (e)
Return False
If _ name _ = '_ main __':
If send_mail (u'this is a python test mail', u'python send mail '):
Print u'sent successfully'
Else:
Print u'sending failed'

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.