iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python学习之使用Python发送邮
  • 573
分享到

Python学习之使用Python发送邮

Python 2023-01-31 01:01:25 573人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

最近写的检查Redis配置的脚本中需要增加一个发送邮件的功能,于是现学现用了python的邮件发送模块smtplib.可以参考《Python for Unix and linux Administrator》一书#/usr/bin/pyth

最近写的检查Redis配置的脚本中需要增加一个发送邮件的功能,于是现学现用了python的邮件发送模块smtplib.可以参考《Python for Unix and linux Administrator》一书

#/usr/bin/python

import smtplib                                #导入smtplib模块

mail_server='smtp.exmail.qq.com'              #定义发送有邮件服务器的地址和端口
mail_server_port=25

from_addr='test@qq.com'                       #定义发送方和接收方地址
to_addr='abc123@qq.com'

from_header='From: %s\r\n' % from_addr        
to_header='To: %s\r\n\r\n' % to_addr
subject_header='Subject: nothint intersting'

body='This is a not-very-interesting email.'

email_message='%s\n%s\n%s\n\n%s' % (from_header,to_header,subject_header,body)

s=smtplib.SMTP(mail_server,mail_server_port)    #连接邮件服务器
#s.set_debuglevel(1)                            #开启debug
s.login("test@qq.com","12345")                  #登录邮件服务器
#s.starttls()                                   
s.sendmail(from_addr,to_addr,email_message)     #发送邮件
s.quit()


显示的结果为:

wKiom1OZlvricG8sAAEa-TrHp5M083.jpg


从上面可以看出以上代码发出的邮件不是我们想要的格式,邮件主题和收件人显示的位置有问题。改成以下的代码

#/usr/bin/python

import smtplib
from email.MIMEMultipart import MIMEMultipart     
from email.MIMEText import MIMEText
#这两个模块可以格式化邮件内容

mail_server='smtp.exmail.qq.com'
mail_server_port=25

mail_user='test@qq.com'
mail_passWord='12345'

from_addr='test@qq.com'
to_addr='abc123@qq.com'
subject_header='Subject: nothint intersting'



body='This is a not-very-interesting email.'


m=MIMEMultipart()                      #格式化邮件内容
m["To"]=to_addr
m["From"]=from_addr
m["Subject"]=subject_header
m.attach(MIMEText(body))


s=smtplib.SMTP(mail_server,mail_server_port)
s.set_debuglevel(1)
s.login(mail_user,mail_password)
#s.starttls()
s.sendmail(from_addr,to_addr,m.as_string())         
s.quit()


显示结果如下:

wKioL1OZmmPxIJLiAADVRAMNaFA179.jpg


现在邮件就能正常显示收件人和主题了。



--结束END--

本文标题: Python学习之使用Python发送邮

本文链接: https://www.lsjlt.com/news/183120.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python学习之使用Python发送邮
    最近写的检查redis配置的脚本中需要增加一个发送邮件的功能,于是现学现用了python的邮件发送模块smtplib.可以参考《Python for Unix and Linux Administrator》一书#/usr/bin/pyth...
    99+
    2023-01-31
    Python
  • python 使用exchange发送邮
    安装库exchangelib pip install exchangelib 脚本内容 # coding=utf-8 # # Created on 2018/2/ from exchangelib import DELEGAT...
    99+
    2023-01-31
    python exchange
  • python学习群发邮件
    https://ke.qq.com/course/109110 腾讯课堂视频 使用Python发送邮件 1、 SMTP 服务器介绍 2、 SMTP 邮件服务器开启 3、 Email 模块介绍 4、 Smtplib.SMTP_SSL介绍 ...
    99+
    2023-01-31
    邮件 python
  • python 使用stmp发送邮件
    SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。 python的smtplib提供了一种很方便的途径发送电子邮件。它对smtp...
    99+
    2023-01-31
    发送邮件 python stmp
  • Python 使用Gmail发送邮件
    前言:2014-05-22记录在hi baidu上,现在移过来 使用python向gmail发邮件 """ 发送邮件 1: 需要提供发送者的邮件、密码;接收者地址; 2:步骤: a:Logi...
    99+
    2023-01-31
    发送邮件 Python Gmail
  • 使用python发送html邮件
    说明:   最近一直在忙着业务迁移工作,己经有些日子没有写东西了,虽然写的很渣,还好是将功能实现了。#!/usr/bin/env python #coding:utf8   import smtplib from email.mime.te...
    99+
    2023-01-31
    邮件 python html
  • python发送邮件
    python通过smtp发送qq邮件 import smtplib from email.mime.text import MIMEText from email.header import Header """ 1》测试邮件发送 ...
    99+
    2023-01-30
    发送邮件 python
  • python 发送邮件
    #!/usr/bin/env python#coding:utf-8 import smtplib,time,stringfrom email.mime.text import MIMEText SMTPserver = 'smtp.exm...
    99+
    2023-01-31
    发送邮件 python
  • python 邮件发送
    环境:python2.7 1 #coding:utf-8 2 from __future__ import unicode_literals 3 __author__ = 'crista' 4 5 import smtpli...
    99+
    2023-01-30
    邮件发送 python
  • python发送、抄送邮件
    python发送抄送邮件 sendemial.py #!/usr/bin/python # -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMETe...
    99+
    2023-01-31
    邮件 python
  • zabbix用python发送邮件
    !/usr/bin/pythoncoding: utf-8import smtplibimport sysfrom email.mime.text import MIMEText_user = "12345678@qq.com"_pwd ...
    99+
    2023-01-31
    发送邮件 zabbix python
  • python之三行代码发送邮件
    (1)首先进入cmd,输入pip install yagmail (2)思路:1 、连接服务器:yagmail.SMTP(邮箱账号,邮箱密码,邮箱服务器地址,邮箱服务器端口)                     2 、准备正文内容:c...
    99+
    2023-01-30
    发送邮件 代码 python
  • python学习之numpy使用
    #NumPy数据库学习#Numpy包含一下特点:'''1.强大的N维数组对象。2.成熟的函数库。3.用于集成c/c++和Fortran代码工具4.实用的线性代数,傅里叶变换和随机生成函数。'''import numpy as np#4.1:...
    99+
    2023-01-31
    python numpy
  • python SMTP邮件发送
    本例使用的时python2.7环境,python3的操作应该也是差不多的。 需要用到smtplib和email两个包。 发送文本类型的邮件 下面看个发送文本邮件的例子(使用网易163的SMTP): # -*- coding: UTF-8 ...
    99+
    2023-01-31
    邮件发送 python SMTP
  • python中使用yagmail发送邮件功能
    1.使用前先要安装 yagmail pip install yagmail -i https://pypi.douban.com/simple 2.使用QQ邮箱发送邮件,使用的是...
    99+
    2024-04-02
  • 怎么用Python发送邮件
    本篇内容主要讲解“怎么用Python发送邮件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用Python发送邮件”吧!Python使用SMTP发送邮件SMTP(Simple Mail Tra...
    99+
    2023-06-04
  • Python 调用API发送邮件
    在运营或者对各种 SDK 或者 API 进行调试的时候,邮件功能基本上都会被使用到。 在测试的时候,可能很多人都会使用 SMTP 或者自己的邮箱使用 SMTP 来进行发送,通常来说是...
    99+
    2024-04-02
  • Python学习之使用Python生成P
    在有些时候运维同事需要对一些数据收集后形成PDF报告的形式发送出去。利用python的reportlab库可以帮我们很快的实现自定义生成PDF报告。在CentOS 下通过sudo yum install python-reportlab -...
    99+
    2023-01-31
    Python
  • Python中如何使用SMTP发送邮件
    本篇文章为大家展示了Python中如何使用SMTP发送邮件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。Python创建 SMTP 对象语法import smtplib smtp...
    99+
    2023-06-02
  • Python中怎么使用SMTP发送邮件
    这篇“Python中怎么使用SMTP发送邮件”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Python中怎么使用SMTP发送...
    99+
    2023-06-28
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作