def send_email(): try: data = app.current_request.json_body # print(data) email = data["email"] password = data["password"] smtp_server = data["smtp_server"] smtp_port = data["smtp_port"] toAddress = data["toAddress"] fromAddress = data["fromAddress"] name = data["name"] subject = data["subject"] bodyPLAIN = data["bodyPLAIN"] bodyHTML = textile(bodyPLAIN) mail = Mail(host=smtp_server, port=smtp_port, username=email, password=password, use_ssl=True) msg = Message() msg.subject = subject msg.fromaddr = (name, fromAddress) msg.to = toAddress msg.body = bodyPLAIN msg.html = bodyHTML # msg.cc = "*****@*****.**" # msg.bcc = ["*****@*****.**", "*****@*****.**"] # msg.reply_to = "*****@*****.**" msg.date = int(round(time.time())) msg.charset = "utf-8" msg.extra_headers = {} msg.mail_options = [] msg.rcpt_options = [] # print(msg) # print(type(msg)) mail.send(msg) return Response(body={'sent': True}, status_code=200, headers=custom_headers) except Exception as error: # print("Send Emails Error") # print(error) return Response(body={'AppError': str(error)}, status_code=500, headers=custom_headers)
# 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'
#!/usr/bin/env python3 # Before begin you need to change Gmail settings: # 1. Turn off 2-step verification # 2. Enable: Allow less secure apps from sender import Mail, Message mail = Mail("smtp.gmail.com", port=587, username="******", password="******", use_tls=True, use_ssl=False, debug_level=None) mail.fromaddr = ("Sender Name", "*****@*****.**") msg = Message("msg subject") msg.fromaddr = ("Sender Name", "*****@*****.**") msg.to = "*****@*****.**" msg.body = "this is a msg plain text body" msg.html = "<b>this is a msg text body</b>" msg.reply_to = "*****@*****.**" msg.charset = "utf-8" msg.extra_headers = {} msg.mail_options = [] msg.rcpt_options = [] # Send message mail.send(msg)