Sunday, May 24, 2020

Send Email in Python Example

Email Send in Python :

So for this first, you have to install the flask-mail library. make sure you are in the same virtual environment.use the following command to install the same.

(venv)$ pip install Flask-Mail

Once installed you have to initiate and configure flask mail.


Code :

cuurent_appp.config - this is just a configuration.

MAIL_DEFAULT_SENDER = 'your_email_address'
MAIL_SERVER = 'email_providers_smtp_address'
MAIL_PORT='mail_server_port'
MAIL_USERNAME='your username'
MAIL_PASSWORD='your password'
MAIL_USE_TLS=False
MAIL_USE_SSL=True


from flask_mail import Message,Mail
mail = Mail()

 for the initiate, it just calls below
 
mail.init(app) 

def send_email(to,subject,template):
    msg = Message(
       subject,
   receipts=[to],
   html=template,
   sender=cuurent_app.config['MAIL_DEFAULT_SENDER']
                   )
   mail.send(msg)
 
Use the above code to send the email. Thanks!



No comments: