邮件自动发送

一.邮件发送基本流程

生活中发送邮件的步骤:注册邮箱账号 -> 打开登录邮箱的网址 -> 输入账号和密码完成登录 -> 构建邮件 -> 发送邮件
连接邮箱服务器,登录邮箱和发送邮件的模块
import smtplib
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.text import MIMEText

1.准备邮箱账号和授权码

username = '3070******@qq.com'
mandate_code = 'ytfsdkvidqbndchj'

2.连接邮箱服务器

con = smtplib.SMTP_SSL('smtp.qq.com')

3.登录邮箱

con.login(username, mandate_code)

4.构建邮箱

1)创建邮件对象(创建空邮件)
msg = MIMEMultipart()
2)添加邮件主题
header = Header('邮件主题', 'utf-8').encode()
msg['Subject'] = header
3)设置发件人显示信息
sender = f'{username} <{username}>'
msg['From'] = sender
4)设置收件人信息
receiver = 'lmx93205****@163.com'
msg['To'] = receiver
5)构建邮件正文
content = MIMEText('邮件正文内容', 'plain', 'utf-8')
msg.attach(content)

5.发送邮件

con.sendmail(username, 'lmx93205****@163.com', msg.as_string())

6.关闭连接

con.quit()

二.添加文件附件

import smtplib
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.text import MIMEText

username = '3070******@qq.com'
mandate_code = 'ytfsdkvidqbndchj'

con = smtplib.SMTP_SSL('smtp.qq.com')

con.login(username, mandate_code)

msg = MIMEMultipart()

header = Header('邮件主题', 'utf-8').encode()
msg['Subject'] = header

sender = f'{username} <{username}>'
msg['From'] = sender

receiver = 'lmx93205****@163.com'
msg['To'] = receiver
plain - 普通文本
content = MIMEText('具体内容请查收附件', 'plain', 'utf-8')
msg.attach(content)
-------------- 添加附件 ---------------
file1 = open('files/smtplib 邮件自动发送.pdf', 'rb').read()
base64 - 文件
att1 = MIMEText(file1, 'base64', 'utf-8')
att1['Content-Disposition'] = 'attachment; filename = "smtplib.pdf"'
msg.attach(att1)

con.sendmail(username, 'lmx93205****@163.com', msg.as_string())
con.quit()

三.发送富文本

import smtplib
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

username = 'Liao****@qq.com'
mandate_code = 'ytfsdkvidqbndchj'

con = smtplib.SMTP_SSL('smtp.qq.com')
con.login(username, mandate_code)

msg = MIMEMultipart()
msg['Subject'] = Header('发送富文本', 'utf-8')
msg['From'] = f'{username} <{username}>'
msg['To'] = 'lmx93205****@163.com'
-------------------------------发送富文本--------------------------------
html - 富文本(超文本)
MIMEText(html代码, ‘html’, 'utf-8)
html = open('files/Demo.html', 'r', encoding='utf-8').read()
content = MIMEText(html, 'html', 'utf-8')
msg.attach(content)

con.sendmail(username, 'lmx93205****@163.com', msg.as_string())
con.quit()

Demo.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <img src="1.jpg" alt="" style="width:760px;">
    <div style="width:674px;">
        <img src="http://mobile.12306.cn/weixin/resources/weixin/images/mail/mail_top.jpg" alt="">
        <br>
        <b>尊敬的旅客:</b>
        <p>您好!</p>
        <p><b>您购买的11月13日G8733次列车因故停运,余婷需在12月12日前登录12306网站或到铁路车站按规定办理未乘区间退票手续,建议优先选择12306网站办理退票手续。</b></p>
        <p>感谢您使用中国铁路客户服务中心网站<a href="https://www.12306.cn/index/">12306.cn</a>! 本邮件由系统自动发出,请勿回复。</p>
        <p>
            <img src="http://mobile.12306.cn/weixin/resources/weixin/images/mail/mail_logo.jpg" alt="">
            中国铁路客户服务中心
        </p>
        <p>2022年11月21日</p>
        <img src="http://mobile.12306.cn/weixin/resources/weixin/images/mail/mail_line.jpg" alt="">
    </div>

</body>
</html>

四.发送图片

import smtplib
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

1.准备账号和授权码

username = 'Liao****@qq.com'
mandate_code = 'ytfsdkvidqbndchj'

2.连接服务器

con = smtplib.SMTP_SSL('smtp.qq.com')

3.登录邮箱

con.login(username, mandate_code)

4.准备邮件

msg = MIMEMultipart()

msg['Subject'] = Header('发送图片', 'utf-8')
msg['From'] = f'{username} <{username}>'
msg['To'] = 'lmx93205****@163.com'
------------------------发送图片-------------------------
file1 = open('files/1.jpg', 'rb').read()
att1 = MIMEText(file1, 'base64', 'utf-8')
att1['Content-Disposition'] = 'attachment;filename="1.jpg"'
msg.attach(att1)

##### 以正文方式发送图片
image_data = open('files/1.jpg', 'rb').read()
image = MIMEImage(image_data)
image.add_header('Content-ID', '<image1>')      # 给图片对象设置图片id
msg.attach(image)

image_data2 = open('files/2.jpg', 'rb').read()
image2 = MIMEImage(image_data2)
image2.add_header('Content-ID', '<image2>')
msg.attach(image2)
在html代码中通过img标签的src属性
content = MIMEText('图片效果如下:<br><img src="cid:image1" alt="" style="width:200px;height:150px"> <br><img '
                   'src="cid:image2" alt="" style="width:200px;height:150px">', 'html', 'utf-8')
msg.attach(content)

con.sendmail(username, 'lmx93205****@163.com', msg.as_string())

五.认识前端

六.常见的标签

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--    设置网页图标-->
    <link rel="icon" href="files/1.jpg">
</head>
<body>
    <!--  1.标题标签: h1 ~h6  -->
    <h1>我是标题1</h1>
    <h1>二级标题</h1>
    <h1>三级标题</h1>
    <h1>四级标题</h1>
    <h1>五级标题</h1>
    <h1>六级标题</h1>

    <!--  2.段落标签  -->
    <P>
        领略文学之力,感受文学之美。11月20日19时30分,由中国作家协会、北京市委宣传部、
        湖南省委宣传部共同主办的“中国文学盛典·鲁迅文学奖之夜”在北京盛大启幕。第八届鲁
        迅文学奖获得者之一,青海省蒙古族青年作家索南才让参会并领奖。
    </P>
    <!--  &emsp:(空一个空格),&nbsp:(空一个像素)  -->
    <p>
        &emsp;&emsp;*****<br><br>
        &emsp;&emsp;*****<br><br>
        &emsp;&emsp;*****<br><br>
        &emsp;&emsp;*****<br><br>
    </p>
    <!--  图片标签  -->
    <img src="https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png">
    <!--  浏览器上显示本地图片  -->
    <img src="files/1.jpg" alt="">
    <img src="files/2.jpg" alt="">
    <!--  邮件中显示本地图片  -->
    <img src="cid:i1">
    <img src="cid:i2">
    <!--  target - 设置跳转方式,默认是 _self  -->
    <a target="_blank" href="https://www.baidu.com/">百度</a>
    <!--    7.input标签    -->
    <!--    7.input标签    -->
    <!--    1)普通文本输入框    -->
    用户名:<input type="text">
    <br><br>

    <!--    2)密码输入框    -->
    密&emsp;码:<input type="password">
    <br><br>

    <!--    3)按钮   -->
    <input type="button" value="登录">
    &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
    <input type="button" value="注册">
    <br><br>

    <!--    4)单选按钮   -->
    <input type="radio" name="a">男
    <input type="radio" name="a">女
    <br><br>

    <!--    5)复选按钮   -->
    <input type="checkbox">记住密码
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值