Package: chalice-mail
One of the most basic functions in a web application is the ability to send emails to your users fully serverlessly.# Maintainers wantedChalice-MailTheChalice-Mailextension provides a simple interface to set up SMTP and AWS SES(Simple Email Service) with your Chalice application and to send messages from your views and scripts.
source code available at:Github RepoUsageUsing SMTP => TLS EncryptionfromchaliceimportChalicefromchalice_mailimportMail,Messageapp=Chalice(app_name='chalice-mail-test1')mail=Mail(app)mail.is_smtp=Truemail.smtp_using_tls=Truemail.username="someone@example.com"mail.password="world's_top_secret_password"mail.smtp_server='smtp.example.com'mail.smtp_port=587mail.login()@app.route('/send-smtp-mail')defsend_smtp_mail():msg=Message(mail)msg.subject="this is the subject"msg.add_recipient("aniketsarkar@yahoo.com")msg.plain="This is the email body."mail.send_email(msg)return{'message':'email sended successfully'}Using SMTP => SSL EncryptionfromchaliceimportChalicefromchalice_mailimportMail,Messageapp=Chalice(app_name='chalice-mail-test1')mail=Mail(app)mail.is_smtp=Truemail.smtp_using_ssl=Truemail.username="someone@example.com"mail.password="world's_top_secret_password"mail.smtp_server='smtp.example.com'mail.smtp_port=465mail.login()@app.route('/send-smtp-mail')defsend_smtp_mail():msg=Message(mail)msg.subject="this is the subject"msg.add_recipient("aniketsarkar@yahoo.com")msg.plain="This is the email body."mail.send_email(msg)return{'message':'email sended successfully'}Using SES(Simple Email Service)fromchaliceimportChalicefromchalice_mailimportMail,Messageapp=Chalice(app_name='chalice-mail-test1')mail=Mail(app)mail.username="someone@example.com"mail.aws_region="ap-south-1""""please provide the value of 'mail.aws_secret_id'and 'mail.aws_secret_key' if you want to set the AWSIam user manually."""mail.login()@app.route('/send-smtp-mail')defsend_smtp_mail():msg=Message(mail)msg.subject="this is the subject"msg.add_recipient("aniketsarkar@yahoo.com")msg.plain="This is the email body."mail.send_email(msg)return{'message':'email sended successfully'}Using SMTP => Email with HTMLfromchaliceimportChalicefromchalice_mailimportMail,Messageapp=Chalice(app_name='chalice-mail-test1')mail=Mail(app)mail.is_smtp=Truemail.smtp_using_tls=Truemail.username="someone@example.com"mail.password="world's_top_secret_password"mail.smtp_server='smtp.example.com'mail.smtp_port=587mail.login()@app.route('/send-smtp-mail')defsend_smtp_mail():msg=Message(mail)msg.subject="this is the subject"msg.add_recipient("aniketsarkar@yahoo.com")"""Simply pass the html value to the html variable of Message instance."""msg.html="<h1>This is the email body.</h1>"mail.send_email(msg)return{'message':'email sended successfully'}Using SMTP => Email with HTML renderingfromchaliceimportChalicefromchalice_mailimportMail,MessagefrompathlibimportPathfromosimportpathapp=Chalice(app_name='chalice-mail-test1')_baseDir=Path(path.realpath(__file__)).parentmail=Mail(app)mail.is_smtp=Truemail.smtp_using_tls=Truemail.username="someone@example.com"mail.password="world's_top_secret_password"mail.smtp_server='smtp.example.com'mail.smtp_port=587mail.template_dir=_baseDir/'templates'# required if using template rendering.mail.login()@app.route('/send-smtp-mail')defsend_smtp_mail():msg=Message(mail)msg.subject="this is the subject"msg.add_recipient("aniketsarkar@yahoo.com")context={}"""This package comes with the jinja2 based template rendering system.Simpley use the 'render_template()' function to rend the html file.'render_template()' functions takes the html file name as argumentsand the context as well as."""msg.html=mail.render_template('index.html',context)mail.send_email(msg)return{'message':'email sended successfully'}Using SMTP => Email with attachmentsfromchaliceimportChalicefromchalice_mailimportMail,MessagefrompathlibimportPathfromosimportpathapp=Chalice(app_name='chalice-mail-test1')_baseDir=Path(path.realpath(__file__)).parentmail=Mail(app)mail.is_smtp=Truemail.smtp_using_tls=Truemail.username="someone@example.com"mail.password="world's_top_secret_password"mail.smtp_server='smtp.example.com'mail.smtp_port=587mail.attachment_dir=_baseDir/'attachments'# required if using attachments.mail.login()@app.route('/send-smtp-mail')defsend_smtp_mail():msg=Message(mail)msg.subject="this is the subject"msg.add_recipient("aniketsarkar@yahoo.com")msg.plain="This is the email body.""""Use the 'add_attachments()' function to add the attachmentswith the message instance. Don't forget to put the attachmentson the attachments folder.'add_attachments()' function basically takes list or str datatype as argument. If you want to add only one attachment just passthe attachment name. If you want to add more than one attachments use a list."""msg.add_attachments(['python.png','README.rst'])mail.send_email(msg)return{'message':'email sended successfully'}Installationchalice-mailis available frompypi.install using pippipinstallchalice-mailinstall from source codegitclonehttps://github.com/marktennyson/chalice-mail&&cdchalice-mail
pythonsetup.pyinstall--userCompatibilitychalice-mailis compatiable with python3.6 and higher versions.
Not compatiable for Python version 2.ContributingWe welcome contributions of all types!