Python发送邮件基础知识与代码讲解!

如何批量发送邮件?小白问百度,大神秀操作,网友惊呼:666 用Python发邮件有哪些优势? 批量发送邮件,并且各邮件可以不同 高度自定义的定时发送 更有效地管理收件箱 基本概念 有关邮件的基本概念,可以查看下图 代码操作前的配置 以网易163邮箱为例,邮箱需要先开通POP3/SMTP/IMAP,可以按照下面步骤执行 注意,之后我们在代码里登录邮箱用的不是账号密码,而是授权码 如果是QQ邮箱,也可类似操作 安装依赖模块 yagmail:基于 SMTP 的发邮件模块 keyring:访问系统密钥环服务,方便、安全. 阅读详情

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取

python免费学习资料、代码以及交流解答点击即可加入


本文主要对邮件操作基础知识及代码进行介绍,用Python发邮件有哪些优势?👇

  • 批量发送邮件,并且各邮件可以不同
  • 高度自定义的定时发送
  • 更有效地管理收件箱

基本概念

有关邮件的基本概念,可以查看下图

 

代码操作前的配置

以网易163邮箱为例,邮箱需要先开通POP3/SMTP/IMAP,可以按照下面步骤执行

 

 

 

 

注意,之后我们在代码里登录邮箱用的不是账号密码,而是授权码

 


如果是QQ邮箱,也可类似操作

 

安装依赖模块

  • yagmail:基于SMTP的发邮件模块
  • keyring:访问系统密钥环服务,方便、安全地储存密码

     

     

    在命令行中键入以下代码可同时安装两个模块:

pip install yagmail keyring --user

具体代码操作

1.发送单个邮件

基本代码逻辑如下
yagmail.SMTP(user=用户名, host=SMTP服务器域名)
yagmail.SMTP(user=用户名, password=授权码, host=SMTP服务器域名)
mail.send(收件人用户名,邮件标题,邮件内容)

所以登陆邮箱并发送一封邮件的代码可以这样写👇

import yagmail
mail = yagmail.SMTP(user='chenx595@163.com', host='smtp.163.com') 
contents = ['第一段内容', '第二段内容'] 
mail.send('chenx595@163.com', '这是一封邮件', contents)

注意,yagmail基础设置中服务器地址需要和所用的邮箱对应!

2. 群发邮件

从下图可以看到,将收件的邮箱组合成列表形式,即可给多个联系人群发邮件

 

3. 发送带有HTML样式的邮件

需要具备一定的前端知识,这里以附带链接为例<a href=链接网址>链接显示的文字</a>

import yagmail
mail = yagmail.SMTP(user='chenx595@163.com', host='smtp.163.com') 
contents = ['第一段', '第二段', '<a href="https://www.baidu.com">某度</a>'] 
mail.send('chenx595@163.com', '带HTML样式的邮件', contents)

4. 发送带有附件的邮件

yagmail非常智能,可以识别contents列表中本地路径作为附件

import yagmail
mail = yagmail.SMTP(user='chenx595@163.com', host='smtp.163.com') 
contents = ['第一段', '第二段', r'C:\xxx\xxx'] 
mail.send('chenx595@163.com', '带HTML样式的邮件', contents)

5. 给邮件正文嵌入图片

这里就出现问题了,我们往往把图片存在本地,如果按上述的代码在内容列表中放图片路径的话,会以附件形式存图片。

那么,如何用代码将图片嵌入正文中呢,用到的代码为:yagmail.inline('图片路径')]

import yagmail
mail = yagmail.SMTP(user='chenx595@163.com', host='smtp.163.com') 
contents = ['第一段', '第二段', yagmail.inline(r'C:\xxx\xxx')] 
mail.send('chenx595@163.com', '带HTML样式的邮件', contents)

对添加附件的代码稍加改动,将你需要发送的图片路径替换即可!

用邮件分割和传送大文件,python实现 III 用邮件分割和传送大文件,python实现 III 4. 接收端baoyu 4.1 导言和import 这部分,是从zhumao.py中抄过来的。 1 #! /usr/bin/python 2 # -*- coding: utf-8 -*- 3  4 # baoyu, 邮件接收者 5 __usage__ = "usage: %baoyu.py [--help]" 6 阅读详情

相关推荐

Python SMTP发送超大附件

Python 邮件如何发送超大附件

weixin_64710229的博客 1746

群发邮件的方法有哪些?怎样大量群发邮件?

邮件群发是邮箱用户们常用到的功能,群发邮件的方法有哪些呢?群发单显、抄送群发、群发密送,或直接添加多个收件人。但怎样大量群发邮件呢?选择一个好的邮箱服务器很重要。 群发邮件的方法有哪些呢? 企业邮箱 TOM企业邮箱的群发邮件方式除了日常发信多放你个收件人外,有群发单显和邮件群发两种方式。 群发单显:一次可群发给400人,收件人收到的邮件只能看到这封邮件发给了自己。一般是公司里有对外活动宣传、公告等,会用到这种形式。 邮件群组:一般公司内部管理使用,其功能是可以把所有人的邮箱像微信群一样建立到一个群组里,只需

huixiana的博客 3万+

如何用python代码发送邮件

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录一、发送邮件前的准备二、如何获取授权码三、编写代码 一、发送邮件前的准备 注册邮箱:要想给别人发邮箱首先自己需要有邮箱哦 基础配置: 要拥有授权码(一定要保存好,这个非常重要) SMTP服务器: smtp.126.com 二、如何获取授权码 1.在邮箱设置中找到 POP3/SMTP/IMAP 这个选项,操作如下图: 2.点开之后会看到 POP3/SMTP服务 将他开启 开启之后会弹出一个窗口,就是要发邮件的授权码 注意.

weixin_56382303的博客 4825

python3发送邮件(格式:html,带多个附件,解决乱码)

python3发送邮件(格式:html,带多个附件,解决乱码) 废话不多说,先来上代码。 import smtplib from email.message import EmailMessage from email.headerregistry import Address, Group import email.policy import mimetypes import base64 class SendEmail(object): """ python3 发送邮件类 格

小太阳xss的博客 3623

Python SMTP发送邮件

smtp发送文件

一个想交朋友的陌生人 1826

讲解:在Python如何使用SMTP发送邮件

SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。 python的smtplib提供了一种很方便的途径发送电子邮件。它对smtp协议进行了简单的封装。 Python创建 SMTP 对象语法 import smtplib smtpObj = smtplib.SMTP( [host [,...

python零基础入门 1744

python发送邮件基础知识与代码讲解!

作者:陈熹 来源:早起Python 前段时间的文章中,我们主要讲解python与Word/Excel/PPT等Office软件的相关操作,现在我们将继续分享如何用Python与更多的办公软件交互! 本文主要对邮件操作基础知识及代码进行介绍,用python发邮件有哪些优势????? 批量发送邮件,并且各邮件可以不同 高度自定义的定时发送 更有效地管理收件箱 基本概念 有关邮件的基本概念,可以查看下图 代码操作前的配置 以网易163邮箱为例,邮箱需要先开通POP3/SMTP/I...

CDA数据分析师 452

树莓派4B+Ubuntu22.04+思岚RPLIDAR C1激光雷达应用

树莓派+Ubuntu22.04+思岚RPLIDAR C1激光雷达简单应用

techfuture的博客 3711

python代码_Python发送邮件基础知识与代码讲解!

↑↑↑关注后"星标"简说Python人人都可以简单入门Python、爬虫、数据分析简说Python推荐来源:早起Python 作者:陈熹Oneoldwatch,likebrief python前言本文主要对邮件操作基础知识及代码进行介绍,用Python发邮件有哪些优势?????批量发送邮件,并且各邮件可以不同高度自定义的定时发送更有效地管理收件箱基本概念有关邮件的基本概念,可以查看下图...

weixin_39673184的博客 179

超详细MySQL 8.0安装与配置

MySQL的安装与配置 还不会安装MySQL的同学们,还不会配置MySQL的同学们,赶紧按照下面步骤操作起来,超详细安装步骤如下: 进入MySQL官网 1、点击download 2、点击MySQL Community Server 3、选好选项点击go to download page 4、点击download 5、点击no thanks,just start my download 6、选Server only,点击Next 7、点击Execute 8、点击

Turniper的博客 8763

VN1630A硬件配置指南:手把手教你用CanApe搭建XCP标定工程(附ELF文件配置技巧)

本文详细介绍了如何使用VN1630A硬件和CanApe软件搭建XCP标定工程,包括硬件配置、CanApe工程创建、ELF文件处理及XCP协议层配置等关键步骤。特别针对实际项目中常见的错误和细节问题提供解决方案,帮助工程师高效完成ECU参数标定工作。

git9versioner的博客 1161

smtp发送图片_python发送邮件基础知识与代码讲解!

作者:陈熹 来源:早起Python前段时间的文章中,我们主要讲解python与Word/Excel/PPT等Office软件的相关操作,现在我们将继续分享如何用Python与更多的办公软件交互!本文主要对邮件操作基础知识及代码进行介绍,用python发邮件有哪些优势? 批量发送邮件,并且各邮件可以不同高度自定义的定时发送更有效地管理收件箱基本概念有关邮件的基本概念,可以查看下图代码操作前的配置以网...

weixin_39880490的博客 909

ADS学习笔记 1. 功率放大器设计

基于ADS2023 update2的学习笔记1.功率放大器设计

u014087103的博客 1万+

python收发邮件的基础知识_Python发送邮件基础知识与代码讲解!

前言大家好,在上个月我们曾经将第一阶段的20篇文章进行整理,????可以点击本文字直达。在前一段时间的文章中,我们主要讲解Python与Word/Excel/PPT等Office软件的相关操作,现在我们将继续分享如何用Python与更多的办公软件交互!本文主要对邮件操作基础知识及代码进行介绍,用Python发邮件有哪些优势?????批量发送邮件,并且各邮件可以不同高度自定义的定时发送更有效地管理收件箱基本概念...

weixin_42514890的博客 485

MuJoCo Playground 机器人强化学习入门教程(一)

MuJoCo Playground 包含 DM 控制套件、机器人运动和机器人操作环境。env每个环境还与一个环境配置相关联,如果需要,可以覆盖该配置。env_cfg请注意,环境配置包含 sim_dt 和 ctrl_dt。ctrl_dt 决定每个 env.step 的时间间隔。因此,每个 env.step 的模拟步长为 ctrl_dt / sim_dt 倍。其他值得注意的参数还有 vision_config,我们将在基于视觉的笔记本中详细讨论!现在,我们将专注于特权观察。

2506_90492529的博客 4619

Python爬虫逆向案例:某云片滑块验证码分析-滑动验证码

今天我们看个滑块。

2201_76125393的博客 1036

java 代码发送邮件添加附件_python发送邮件基础知识与代码讲解!

作者:陈熹

weixin_39849888的博客 69

FATFS挂载多个存储设备(2):功能配置及功能测试

本文介绍了基于FATFS文件系统实现多存储设备挂载的方法,包含底层设备驱动函数编写、功能配置、功能测试。

kevin1499的博客 2588

python发送邮件

在某些是否我们脚本能发送消息,比如监控电脑性能,满足条件后发送短信或者邮箱通知我们,但是短信门槛高且有费用,所以邮箱更方便

xu.hyj 1万+

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

python之发送邮件

import smtplib from email.mime.text import MIMEText mailserver = "smtp.126.com" #邮箱服务器地址 username_send = 'kindenough@126.com' #邮箱用户名 password = '*' #邮箱密码:需要使用授权码 username_recv = '503690160@qq.co...

日志 9574

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

python自动发送邮件实现

使用python来实现自动发送邮件的功能,本质上也是模拟我们实操的这一过程。 用python来实现邮件的定期发送,可以极大的节省人工成本。

积跬步,慕至千里的博客 5923

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

如何用Python发送邮件?

让邮箱成为你的工作利器。

ZackSock的博客 3666

python判断邮件是否发送成功_9行代码,教你用Python发送电子邮件!

点击“简说Python”,选择“置顶/星标公众号”福利干货,第一时间送达!本文授权转载自数据分析1480,禁二次转载前言大概是在两三个月前,陆续有几位在职朋友问我如何使用Python发送邮件,说心里话这方面的操作在我之前的工作履历中真的没有碰到,所以我没有很好地帮助他们。但这个问题一直埋在我的心底,如果将来我也碰到了这个问题怎么办?总不能临时抱佛脚吧,必须得未雨绸缪。所以这一段时间里,一...

weixin_39802962的博客 2492
上一篇: 区区几行Python代码,一分钟搞定一天工作量
下一篇: 超全整理|Python 操作 Excel 库 xlwings 常用操作详解!
酸菜鱼编程
博客等级 码龄6年 155粉丝 63原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值