吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1493|回复: 12
收起左侧

[讨论] python 发送邮件

  [复制链接]
shyai 发表于 2021-12-16 12:23
本人刚学习python,用python写一些小工具,如有写的不好的地方,请大家多多包含!!


config.ini内容如下:

[sentmail]
#邮箱账号(发件人)
sender=
#邮箱密码
password=
#添加收件人如多个收件人用逗号分割如receivers=a@qq.com,b@qq.com
receivers=xxx@xx.com

SendMail.py内容如下:
import smtplib
from configparser import ConfigParser
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import os

def SendMail(subject,sendfilepath,text_info):
    subject = subject
    sendfilepath = sendfilepath
   #邮件内容
    text_info = text_info
    #加载配置文件
    config = ConfigParser()
    cfg = config.read('config.ini', encoding='utf-8')
    sender = config.get('sentmail', 'sender')
    password = config.get('sentmail', 'password')
    receivers = config.get('sentmail', 'receivers')
    #定义邮件的基本格式
    msg = MIMEMultipart('mixed')
    msg["subject"] = Header(subject, 'utf-8')
    msg["From"] = sender
    receivers = [i.strip() for i in receivers.split(',')]
    msg["To"] = ','.join(receivers)
    fname = os.path.basename(sendfilepath)


    #添加文本信息
    text_info = MIMEText(text_info, 'plain', 'utf-8')
    msg.attach(text_info)


    #添加附件
    file = open(sendfilepath, 'rb').read()
    att = MIMEText(file, 'base64', 'utf-8')
    att["Content_type"] = 'application/octet-stream'
    att.add_header('Content-Disposition', 'attachment', filename = fname)
    msg.attach(att)


    #发送邮件
    try:
        smtpObj = smtplib.SMTP(“填写你使用邮箱SMTP”)
        smtpObj.login(user=sender, password=password)
        smtpObj.sendmail(sender, receivers, msg.as_string())
        smtpObj.quit()
        print ("邮件发送成功")
    except Exception as e:
        print ("Error: 无法发送邮件")

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
diy606 + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

我今天是大佬 发表于 2021-12-16 12:28
有库你不用...
头像被屏蔽
细水流长 发表于 2021-12-16 12:52
rustyjackal 发表于 2021-12-16 12:54
Alexxxx 发表于 2021-12-16 13:00
厉害!!
littleworm 发表于 2021-12-16 13:04
邮箱练手挺好的  谢谢分享
diy606 发表于 2021-12-16 13:35
支持新人多发原创帖子
好学 发表于 2021-12-16 13:39
不错,之前写了一个监控网站 然后发邮件的
laoliu666 发表于 2021-12-16 13:42
支持一下楼主
Atlantis908 发表于 2021-12-16 13:43
我有邮箱账号密码,我为啥还要写个程序来发邮件呢
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-24 05:03

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表