SMTP basically stand for (simple mail transfer protocol) is an application layer protocol that transfers an email from one user to another user.We need to send lots of plain text email and also an email with reports,images, text data, application form data, etc to the business client from a different platform.So if we want to perform task and do it through the script it will save lots of time and cost.In simple an SMTP email is just an email sent using the SMTP server.
Python provides a built-in library for sending an email via SMTP protocol. We can send an email using the smtplib python built-in module. No extra installation required for this.Python comes with the built-in smtplib module for sending emails using the Simple Mail Transfer Protocol (SMTP). smtplib uses the RFC 821 protocol for SMTP. The examples in this tutorial will use the Gmail SMTP server to send emails, but the same principles apply to other email services.For more details you can prefer this link.
- Python version: 3.4
- Pip version: 1.5
- Smtplib version: 3.4
How to send email from gmail?
Give access on google
- To send an email through Gmail in python we need to do some configuration because google does not allow us to send an email via any script without configuration. We need to turn on Less secure app access. Click the below link to turn on the less secure app access and please make sure that you haven’t enabled two-factor authentication because you can’t turn on the less secure app access if you have enabled two-factor authentication.
- After completing your authentication part now you can start with code.
- So let's see now how to send email using python smtplib in below code.
Installation of smtplib
- To get started with emails we first have to do the installation of smtplib.so lets see how to install smtplib?
- Download python,from https://www.python.org/downloads/ and you can prefer this link for further installation..
- Install SMTPlib with the below command.
pip install smtplib
Sending simple test mail using smtplib
import smtplib smtplibObj = smtplib.SMTP("smtp.gmail.com",587) smtplibObj.ehlo() smtplibObj.starttls() smtplibObj.login("email@example.com","password") smtplibObj.sendmail("firstname.lastname@example.org","email@example.com","Subject:Hellow\nHow are you?") smtplibObj.quit()
Imported the `smtplib` to send an email
It turns on secure connection.
You have to add your authentic email Id and password from which you are going to send email.
It is basically divided in three parts “From” it means from which account you are going to send email “To” it means to whom you are sending email and the last part in it is “Subject” in this you can add what you want to send as a message.
Sending email using MimePost
Send via SMTP
You can follow the above steps for installing smtplib.
To find your SMTP details on mimepost, Please visit here:https://docs.mimepost.com/smtp/
Let’s took an example
import smtplib smtplibObj=smtplib.SMTP("smtp.mimepost.com",2525) smtplibObj.ehlo() smtplibObj.starttls() smtplibObj.login("SMTP_USERNAME","SMTP_PASSWORD") smtplibObj.sendmail("firstname.lastname@example.org","email@example.com","Subject:Hellow\nTest mail") smtplibObj.quit()
Send email using mimepost-pythong-sdk
Install Mimepost Python SDK
Generate API token from MimePost
Refer below link to generate API token.
Send Email Example
from __future__ import print_function import time import MimePost from MimePost.rest import ApiException from pprint import pprint # Configure API key authorization: api_key configuration = MimePost.Configuration() configuration.api_key['X-Auth-Token'] = 'YOUR_API_TOKEN' # Uncomment below to setup prefix (e.g. Bearer) for API key, if needed # configuration.api_key_prefix['X-Auth-Token'] = 'Bearer' # create an instance of the API class api_instance = MimePost.AccountsApi(MimePost.ApiClient(configuration)) Try: # Get account profile details api_response = api_instance.account_profile_get() pprint(api_response) except ApiException as e: print("Exception when calling AccountsApi->account_profile_get: %s\n" % e)
For more API integration refer below links
We have successfully sent an smtp email from gmail via mimepost.