python 发邮件:sina普通版   | zabbix版本

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

def send_mail(sender_sina=‘‘,pwd=‘‘,receiver=‘‘,mail_title=‘‘,mail_content=‘‘):
    # 邮箱smtp服务器
    host_server = ‘smtp.sina.com‘
    sender_sina_mail = sender_sina+‘@sina.com‘
    #ssl登录
    smtp = SMTP_SSL(host_server)
    #set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
    smtp.set_debuglevel(0)
    smtp.ehlo(host_server)
    smtp.login(sender_sina, pwd)
    msg = MIMEText(mail_content, "plain", ‘utf-8‘)
    msg["Subject"] = Header(mail_title, ‘utf-8‘)
    msg["From"] = sender_sina_mail
    msg["To"] = receiver
    smtp.sendmail(sender_sina_mail, receiver, msg.as_string())
    smtp.quit()
send_mail("hequan2011","密码","[email protected]","标题",‘内容‘)
#!/usr/bin/python
#coding:utf-8
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
import sys

def send_mail(sender_sina=‘‘,pwd=‘‘,receiver=‘‘,mail_title=‘‘,mail_content=‘‘):
    host_server = ‘smtp.sina.com‘
    sender_sina_mail = sender_sina+‘@sina.com‘
    #ssl登录
    smtp = SMTP_SSL(host_server)
    #set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
    smtp.set_debuglevel(0)
    smtp.ehlo(host_server)
    smtp.login(sender_sina, pwd)
    try:
        msg = MIMEText(mail_content, "plain", ‘utf-8‘)
        msg["Subject"] = Header(mail_title, ‘utf-8‘)
        msg["From"] = sender_sina_mail
        msg["To"] = receiver
        smtp.sendmail(sender_sina_mail, receiver, msg.as_string())
        smtp.quit()
        print("发送成功")
        return  True
    except  Exception as e :
        print("发送失败:",e)
        return False
        
if __name__=="__main__":
    #send_mail("hequan2011","密码","[email protected]","标题",‘内容‘)
    send_mail("hequan2011", "密码", sys.argv[1], sys.argv[2], sys.argv[3])
时间: 2024-07-28 17:07:05

python 发邮件:sina普通版   | zabbix版本的相关文章

python 发邮件乱码

来自:http://outofmemory.cn/code-snippet/1464/python-send-youjian-resolve-suoyou-luanma-question 使用python发邮件很简单,但是遇到乱码问题很烦恼. 乱码问题有几种:有发件人名称乱码,有标题乱码,也有正文乱码的问题. 要解决发件人名称乱码问题,必须使用Header,如下代码: from email.header import Header from = ("%s<[email protected]

48. Python 发邮件(1)

python发送邮件 1.通过python发邮件步骤: 前提:开通了第三方授权,可以使用smtp服务 1.创建smtp对象 2.连接smtp服务器,默认端口号都是25 3.登陆自己的邮箱账号 4.调用发送消息函数,参数:发件人.收件人.消息内容 5.关闭连接 2.邮件消息注册: 首先创建一个消息对象: msg = email.mime.multipart.MIMEMultipart()                     #通过这个类创建消息 msg['from'] = '[email pr

Python 发邮件例子

Python 发邮件例子 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019-04-23 16:12:33 # @Author : BenLam # @Link : https://www.cnblogs.com/BenLam/ import smtplib from email.mime.text import MIMEText from email.header import Header from email.mi

python发邮件出现乱码

decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换成unicode编码. encode的作用是将unicode编码转换成其他编码的字符串,如str2.encode('gb2312'),表示将unicode编码的字符串str2转换成gb2312编码. 另外对于一些包含特殊字符的编码,直接解码可能会报错,可以使用对于的参数来设置.如:s.decode("utf-8", "igno

python发邮件

import smtplib from email.mime.text import MIMEText from email.header import Header # 第三方 SMTP 服务 mail_host="smtp.qq.com" #设置服务器 mail_user="xxxxxx" #用户名 mail_pass="plcfthkdtpoxcabh" #口令QQ需要授权码 sender = '[email protected]' rec

49. Python 发邮件(2)

继续修改上面一节的发邮件代码 发送附件: (1)先找一个本地的文件 (2)打开文件,读出文件字符串 (3)通过MIMT ext()类来创建一个对象att,传入文件读出内容 (4)增加att的头部信息,并指定文件名字 (5)添加到msg消息中msg.attach(att) 样例: attfile = 'test.py' basename = os.path.basename(attfile) fp = open(attfile, 'rb') att = email.mime.text.MIMETe

python 发邮件脚本

相关模块介绍 发送邮件主要用到了smtplib和email两个模块,这里首先就两个模块进行一下简单的介绍:    1.smtplib模块 smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])   SMTP类构造函数,表示与SMTP服务器之间的连接,通过这个连接可以向smtp服务器发送指令,执行相关操作(如:登陆.发送邮件).所有参数都是可选的. host:smtp服务器主机名 port:smtp服务的端口,默认是25:如果在创建SMT

人生苦短之Python发邮件

#coding=utf-8 import smtplib from email.mime.base import MIMEBase from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText ''' 一些常用邮箱发件服务器及端口号 邮箱 发件服务器 非SSL协议端口 SSL协议端口 163 smtp.163.co

Python发邮件实例,并用Tkinter实现UI

一个偶然的机会我接触到Python语言,个人感觉Python是强大的工具语言.就下决心好好研究一下.废话少说,讲述下这个Demo的实现过程及源码.多谢指正! 第一步:最主要的工具包就是:smtplib.下面是基本的操作的核心代码: <span style="font-size:14px;"># -*- coding: utf-8 -*- # Import the email modules we'll need import smtplib from email.mime.