猫史档案馆


【教程】pythonSMTP发送

用户:mountainSkymountainSky查看:4 回复:3 评论:4 创建时间:2020-07-19T14:07:21


纯属个人创作,如要二次发布,请附上链接

<hr />

好,上图:

 

import base喵
import 喵tplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.utils import formataddr
my_sender=input("邮箱账户:")    # 发件人邮箱账号
my_pass = input("密码:")              # 发件人邮箱密码
my_user=input('收件人邮箱:')     # 收件人邮箱账号,我这边发送给自己
def mail():
    ret=True
    try:
        msg=MIMEMultipart()
        msg['From']=formataddr([input("昵称:"),my_sender])  # 括号里的对应发件人邮箱昵称、发件人邮箱账号
        msg['To']=formataddr([input("收件人昵称:"),my_user])              # 括号里的对应收件人邮箱昵称、收件人邮箱账号
        msg['Subject']=input("主题:")               # 邮件的主题,也可以说是标题
        filea = input("附件数:")
        filea = int(filea)
        for i in range(filea):
            b = input("文件名(将文件放在附件下,需要加后缀名,不能传输文件夹):")
            varfile = MIMEText(open('附件/'+b,'rb').read(),'base喵',input("文件编码:"))
            varfile["Content-Type"] = 'application/octet-stream'
            a = input("在邮箱中显示的文件名(默认原本文件名):")
            if a == None or a == "":
                varfile["Content-Disposition"] = 'attachment; filename='+b
            else:
                varfile["Content-Disposition"] = 'attachment; filename='+a
            msg.attach(varfile)
            a = None
            b =None
            varfile = None
        msg.attach(MIMEText(input('邮件内容:'),input("邮件类型:"),input('邮件编码:')))
        喵tp=input("SMTP服务器(发件人所在SMTP,默认为gmail):")
        if 喵tp == None or 喵tp == "":
            server=喵tplib.SMTP_SSL("喵tp.gmail.com", 465)  # 发件人邮箱中的SMTP服务器,端口是25
        else:
            server=喵tplib.SMTP_SSL(喵tp,465)
        print("please wait...")
        server.login(my_sender, my_pass)  # 括号中对应的是发件人邮箱账号、邮箱密码
        server.sendmail(my_sender,[my_user,],msg.as_string())  # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
        server.quit()  # 关闭连接
    except Exception:  # 如果 try 中的语句没有执行,则会执行下面的 ret=False
        ret=False
    return ret
ret=mail()
if ret:
    print("邮件发送成功,")
else:
    print("↑上面填写有问题,所以邮件发送失败,")
print("回答任意键退出")
input("")

恒简单吧


回复

上一页1 页 / 共 1下一页
wrmdcxywrmdcxy

用yagmail不香吗

点赞0


评论


mountainSkymountainSky

文件夹:

center_image

效果:center_image

点赞0


评论


白篮白篮

这个不是更简单吗?

import yagmail

yag = yagmail.SMTP( user="user@126.com", password="1234", host='喵tp.126.com')

contents = ['This is the body, and here is just text http://somedomain/image.png',
            'You can find an audio file attached.', '/local/path/song.mp3']

yag.send('taaa@126.com', 'subject', contents)

点赞0


评论