def send(text, subject=None, toAdd=None, cc=None, log_file=None): mail_info = config.config_dic['mail_info'] sslPort = 465 server = mail_info.get('server') user = mail_info.get('user') passwd = mail_info.get('password') if not (server and user and passwd): print('Invalid login info, exit!') return mail = Mail(server, port=sslPort, username=user, password=passwd, use_tls=False, use_ssl=True, debug_level=None) msg = Message(subject) msg.fromaddr = (user, user) msg.to = toAdd if cc: msg.cc = cc if log_file: zip_path = '/tmp/build.log.zip' zip_file(log_file, zip_path) with open(zip_path, encoding='ISO-8859-1') as f: attachment = Attachment("build.log.zip", "application/octet-stream", f.read()) msg.attach(attachment) msg.body = text msg.charset = "utf-8" mail.send(msg)
def send(text, subject=None, cc=None, toAdd=None, log_file=None): mail_info = config.config_dic['mail_info'] sslPort = 465 server = mail_info.get('server') user = mail_info.get('user') passwd = mail_info.get('password') if not (server and user and passwd): print('Invalid login info, exit!') return mail = Mail(server, port=sslPort, username=user, password=passwd, use_tls=False, use_ssl=True, debug_level=None) msg = Message(subject) msg.fromaddr = (user, user) msg.to = toAdd if cc: msg.cc = cc if log_file: with open(log_file) as f: attachment = Attachment("build.log", "text/plain", f.read()) msg.attach(attachment) msg.body = text msg.charset = "utf-8" mail.send(msg)
def run(arguments): if arguments['--config'] or (not os.path.isfile(CONF)): conf() with open(CONF, 'rb') as f: smtp = json.loads(f.read()) mail = Mail(host=smtp['server'], username=smtp['user'], password=smtp['password'], port=int(smtp['port']), fromaddr=smtp['from']) msg = Message(arguments['--subject'], fromaddr=smtp['from'], body=arguments['--message']) to = arguments['--to'] if to: msg.to = to.split(';') cc = arguments['--cc'] if cc: msg.cc = cc.split(';') bcc = arguments['--bcc'] if bcc: msg.bcc = bcc.split(';') atta = arguments['--attach'] if atta: msg.attach(atta.split(';')) mail.send(msg)
def test_attach(self): msg = Message() att = Attachment() atts = [Attachment() for i in range(3)] msg.attach(att) self.assert_equal(msg.attachments, [att]) msg.attach(atts) self.assert_equal(msg.attachments, [att] + atts)
def send(text, subject, toAdd, cc=None, attachments=None): """邮件发送方法 Arguments: text {str} -- 邮件内容,可以为字符串或者 HTML 字符串 subject {str} -- 邮件标题 toAdd {list} -- 收件人列表 Keyword Arguments: cc {list} -- 抄送人列表 (default: {None}) attachments {list} -- 附件列表,示例:[{'path': '/path/to/file', 'file_name': '附件文件名'}] (default: {None}) """ assert subject is not None assert toAdd is not None sslPort = os.getenv('MAIL_SERVER_PORT') server = os.getenv('MAIL_SMTP_SERVER') user = os.getenv('MAIL_USER') passwd = os.getenv('MAIL_PASSWORD') assert server is not None assert user is not None assert passwd is not None if not sslPort: sslPort = 465 mail = Mail(server, port=sslPort, username=user, password=passwd, use_tls=False, use_ssl=True, debug_level=None) msg = Message(subject) msg.fromaddr = (user, user) msg.to = toAdd if cc: msg.cc = cc if attachments: for att in attachments: with open(att['path'], 'rb') as f: mail_attachment = Attachment(att['file_name'], "application/octet-stream", f.read()) msg.attach(mail_attachment) msg.html = text msg.charset = "utf-8" mail.send(msg)
username=SMTP_USER, password=SMTP_PASS, fromaddr=SMTP_ADDRESS) msg01 = Message("Hello01", to="*****@*****.**", body="hello world") msg02 = Message("Hello02", to="*****@*****.**") msg02.fromaddr = ('no-reply', '*****@*****.**') msg02.body = "hello world!" msg03 = Message("Hello03", to="*****@*****.**") msg03.fromaddr = (u'请勿回复', '*****@*****.**') msg03.body = u"你好世界" # Chinese :) msg03.html = u"<b>你好世界</b>" msg04 = Message("Hello04", body="Hello world 04") msg04.to = "*****@*****.**" msg04.cc = ["*****@*****.**", "cc02@example"] msg04.bcc = ["*****@*****.**"] msg05 = Message("Hello05", to="*****@*****.**", body="Hello world 05") with open("../docs/_static/sender.png") as f: msg05.attach_attachment("sender.png", "image/png", f.read()) msg06 = Message("Hello06", to="*****@*****.**", body="Hello world 06") with open("test.txt") as f: attachment = Attachment("test.txt", "text/plain", f.read()) msg06.attach(attachment) mail.send([msg01, msg02, msg03, msg04, msg05, msg06])
# in email_server_config.txt, you enter your email information in this format: smtp.example.com,25,username,password with open('./email_server_config.txt','r') as f: config_content=[] for line in f.readlines(): config_content=line.split(",") smtp_server=config_content[0] smtp_server_port=config_content[1] smtp_server_username=config_content[2] smtp_server_password=config_content[3] mail=Mail(smtp_server,port=smtp_server_port,username=smtp_server_username,password=smtp_server_password,\ use_tls=False,use_ssl=False,debug_level=None) from sender import Message with open('./to_email_list.txt','r') as f: for line in f.readlines(): msg=Message("email subject",fromaddr=("burness","*****@*****.**"),to=line) msg.body = "this is a msg plain text body" msg.date = time.time() msg.charset = "utf-8" msg.extra_headers = {} msg.mail_options = [] msg.rcpt_options = [] from sender import Attachment with open("to_email_list.txt") as f: attachment=Attachment("to_email_list.txt","text/txt",f.read()) msg.attach(attachment) mail.send(msg) print 'To check in your email that ensure it is ok'
msg02 = Message("Hello02", to="*****@*****.**") msg02.fromaddr = ('no-reply', '*****@*****.**') msg02.body = "hello world!" msg03 = Message("Hello03", to="*****@*****.**") msg03.fromaddr = (u'请勿回复', '*****@*****.**') msg03.body = u"你好世界" # Chinese :) msg03.html = u"<b>你好世界</b>" msg04 = Message("Hello04", body="Hello world 04") msg04.to = "*****@*****.**" msg04.cc = ["*****@*****.**", "cc02@example"] msg04.bcc = ["*****@*****.**"] msg05 = Message("Hello05", to="*****@*****.**", body="Hello world 05") with open("../docs/_static/sender.png") as f: msg05.attach_attachment("sender.png", "image/png", f.read()) msg06 = Message("Hello06", to="*****@*****.**", body="Hello world 06") with open("test.txt") as f: attachment = Attachment("test.txt", "text/plain", f.read()) msg06.attach(attachment) mail.send([msg01, msg02, msg03, msg04, msg05, msg06])