Created
December 23, 2019 07:13
-
-
Save 521xueweihan/02e280e736c43c82f17753529fda10e2 to your computer and use it in GitHub Desktop.
邮件发送工具方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
# | |
# Author : XueWeiHan | |
# Date : 18/8/11 上午11:21 | |
# Desc : 通用工具类 | |
import smtplib | |
from email.mime.text import MIMEText | |
from email.header import Header | |
import requests | |
from config import MAIL, SENDCLOUD | |
def sendcloud_email(**kwargs): | |
log = kwargs['log'] | |
try: | |
url = "http://api.sendcloud.net/apiv2/mail/send" | |
receivers = ';'.join(kwargs['receivers']) | |
params = { | |
"apiUser": SENDCLOUD['api_user'], # 使用api_user和api_key进行验证 | |
"apiKey": SENDCLOUD['api_key'], | |
"to": receivers, # 收件人地址, 用正确邮件地址替代, 多个地址用';'分隔 | |
"from": SENDCLOUD['mail'], # 发信人, 用正确邮件地址替代 | |
"fromName": kwargs['_from'], | |
"subject": kwargs['subject'], | |
"html": kwargs['content'] | |
} | |
r = requests.post(url, data=params, timeout=20) | |
log.info('{} send to {}, {} email finish.'.format( | |
kwargs['mail_service_name'], receivers, kwargs['subject'])) | |
return True | |
except Exception as e: | |
log.exception(e) | |
error_message = '{} send {} email fail. {}'.format( | |
kwargs['mail_service_name'], kwargs['subject'], e.message) | |
if log: | |
log.error(error_message) | |
else: | |
print(error_message) | |
return False | |
def base_email(**kwargs): | |
""" | |
QQ等邮件服务 | |
""" | |
log = kwargs['log'] | |
try: | |
# 发送邮件,邮箱的信息 | |
mail_config = MAIL[kwargs['mail_service_name']] | |
sender = mail_config['mail'] # 发送邮件的邮箱 | |
# 三个参数:第一个为文本内容,第二个 html 设置文本格式,第三个 utf-8 设置编码 | |
message = MIMEText(kwargs['content'], 'html', 'utf-8') | |
message['From'] = Header(kwargs['_from'], 'utf-8') | |
message['Subject'] = Header(kwargs['subject'], 'utf-8') | |
smtp_obj = smtplib.SMTP_SSL() # qq邮箱要求是https连接,所以需要用SMTP_SSL | |
smtp_obj.connect(mail_config['host'], | |
mail_config['port']) # 设置SMTP地址和端口号 | |
smtp_obj.login(mail_config['username'], mail_config['password']) | |
smtp_obj.sendmail(sender, kwargs['receivers'], message.as_string()) | |
smtp_obj.close() | |
log.info('{} send to {}, {} email finish.'.format( | |
kwargs['mail_service_name'], ';'.join(kwargs['receivers']), | |
kwargs['subject'])) | |
return True | |
except Exception as e: | |
log.exception(e) | |
error_message = '{} send {} email fail. {}'.format( | |
kwargs['mail_service_name'], kwargs['subject'], e.message) | |
if log: | |
log.error(error_message) | |
else: | |
print(error_message) | |
return False | |
def send_email(**kwargs): | |
""" | |
发送邮件 | |
:param mail_service_name: | |
:param subject: | |
:param _from: | |
:param receivers: | |
:param content: | |
:param log: | |
:return: | |
""" | |
if kwargs['mail_service_name'] is 'sendcloud': | |
return sendcloud_email(**kwargs) | |
else: | |
return base_email(**kwargs) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment