Send Email using Python through Gmail, Sendgrid and Elasticmail
We have the lot of use case to send the email through the code. For that purpose, I had post this to send email / notification through the python smtp.
ElasticMail SMTP detail - https://elasticemail.com/resources/settings/smtp-api/
Description
We have to send the email using the python code. That will use the third party smtp library to send email. For this we have to get the smtp configuration from the email service provider. Based on the configuration we can send the normal email or attached one. But this will help us to send the marketing email / notification and other purpose.
Pre-requesting
Have to install python on your machine. And have to install smtplib library with the help of pip or other tool. Have to get the gmail smtp information
Use cases :
Now a days with out email notification none of the application delivered. That would help to boom the sales or reach out the right customer.
Sample code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import smtplib; import email.utils def send_email(): gmail_user = "ramachandran.ideas2it@gmail.com" gmail_pwd = "*************" FROM = 'Ramachandran.K' TO = ['rama******@gmail.com', 'rama******@yahoo.in', 'kaavannan*****@gmail.com',' kaavannan*****@gmail.com'] #must be a list SUBJECT = "Testing Mail sent via Python" TEXT = "The mail may contain the warm message so please be secure while reading.... " # Prepare actual message message = """\From: %s\nTo: %s\nSubject: %s\n\n%s """ % (FROM, ", ".join(TO), SUBJECT, TEXT) try: server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo() server.starttls() server.login(gmail_user, gmail_pwd) server.sendmail(FROM, TO, message) #server.quit() server.close() print 'successfully sent the mail' except Exception as ex: print "failed to send mail",ex # Sending email send_email() |
The above code Send the mail to Listed to address using the gmail smtp mail server. The login credential is your gmail user name and password.
Then the to address must be a list.
Need to configure the smtp server to gmail smtp server. for example smtp.gmail.com:587
Then call the login method to process login in gmail account. after that we need to send the mail so call the sendmail() function.
Note :
we can send the email using the different smtp provider. The some SMTP providers detail given below,
Gmail SMTP detail - the detail link is https://support.google.com/a/answer/176600?hl=en
SendGrid SMTP detail - the detail link is https://sendgrid.com/docs/API_Reference/SMTP_API/integrating_with_the_smtp_api.html
Then the to address must be a list.
Need to configure the smtp server to gmail smtp server. for example smtp.gmail.com:587
Then call the login method to process login in gmail account. after that we need to send the mail so call the sendmail() function.
Note :
we can send the email using the different smtp provider. The some SMTP providers detail given below,
Gmail SMTP detail - the detail link is https://support.google.com/a/answer/176600?hl=en
SendGrid SMTP detail - the detail link is https://sendgrid.com/docs/API_Reference/SMTP_API/integrating_with_the_smtp_api.html
ElasticMail SMTP detail - https://elasticemail.com/resources/settings/smtp-api/
Comments
Post a Comment