python2 读取邮件配置,发送邮件

 

[email]
smtpserver=xxx
username=xxx
password=xxx
sender=xxx
receiver=xxx

 

 

import configparser
import os
import smtplib
from email.mime.text import *
from email.mime.multipart import *
from email.header import *

class readConfig():
    def __init__(self):
        f=open("config.ini")
        data=f.read()
        
        
        self.conf = configparser.ConfigParser()
        self.conf.read("config.ini")
    
    
    def getEmailValue(self,name):    
        value = self.conf.get("email",name)
        return value
    
    
    def getUserValue(self,name):
        value = self.conf.get("user",name)
        return value


def sendEmail(file_new):
    conf = readConfig()
    smtpserver = conf.getEmailValue("smtpserver")
    sender = conf.getEmailValue("sender")
    receiver = conf.getEmailValue("receiver")
    username = conf.getEmailValue("username")
    password = conf.getEmailValue("password")
    
    f = open(file_new)
    mail_body = f.read()
    
    msg = MIMEMultipart()
    msg["Subject"] = Header('测试发送邮件', 'utf-8')
    msg_text = MIMEText(mail_body,"html","utf-8")
    msg.attach(msg_text)
    
    msg_file = MIMEText(mail_body,"html","utf-8")
    msg_file["Content-Type"] = 'application/octet-stream'
    msg_file["Content-Disposition"] = 'attachment; filename="testcase.html"'
    msg.attach(msg_file)
    
    msg["from"] = sender
    msg["to"]= receiver
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)
    smtp.login(username, password)
    smtp.sendmail(msg['from'], receiver, msg.as_string())  # 发送邮件
    smtp.quit()
    print('sendmail success')
    
    
if __name__ == '__main__':
    a=sendEmail("testcase.html")

 

转载于:https://www.cnblogs.com/dmtz/p/11088399.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值