visit
Pros of using SMTP
Cons of using SMTP
Pros of transactional email services
Cons of transactional email services
Pros of multichannel notifications services
Cons of multichannel notifications services
You can use Python’s smtplib
module to send email using SMTP (Simple Mail Transfer Protocol), which is an application-level protocol. Note that the module makes use of the protocol for SMTP. I’ll show you how to use Gmail’s SMTP server for this walkthrough.
3. Import smtplib
. Since Python comes pre-packaged with smtplib
, you have to create a Python file and import smtplib
into it.
4. To create a secure connection, you can either use SMTP_SSL()
with 465 port or .starttls()
with 587 port. The former creates an SMTP connection that is secured from the beginning. The latter creates an unsecured SMTP connection that is encrypted via .starttls()
.
To send email through SMTP_SSL()
:
import smtplib
gmail_user = '[email protected]'
gmail_password = 'your_password'
sent_from = gmail_user
to = ['[email protected]', '[email protected]']
subject = 'Lorem ipsum dolor sit amet'
body = 'consectetur adipiscing elit'
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
smtp_server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
smtp_server.ehlo()
smtp_server.login(gmail_user, gmail_password)
smtp_server.sendmail(sent_from, to, email_text)
smtp_server.close()
print ("Email sent successfully!")
except Exception as ex:
print ("Something went wrong….",ex)
To send email through .starttls()
:
import smtplib
try:
#Create your SMTP session
smtp = smtplib.SMTP('smtp.gmail.com', 587)
#Use TLS to add security
smtp.starttls()
#User Authentication
smtp.login("sender_email_id","sender_email_id_password")
#Defining The Message
message = "Message_you_need_to_send"
#Sending the Email
smtp.sendmail("sender_email_id", "receiyer_email_id",message)
#Terminating the session
smtp.quit()
print ("Email sent successfully!")
except Exception as ex:
print("Something went wrong....",ex)
Now that you've initiated a secured SMTP connection, you can move forward and write your message and pass it to .sendmail()
.
Generate and store a SendGrid API key and provide full access to Mail Send permissions.
Create a Python script and start using the API. To begin using SendGrid’s API via Python, follow these steps:
1. To install the sendgrid
package on your machine, refer to SendGrid's or directly install via pip install sendgrid
.
import sendgrid
import os
from sendgrid.helpers.mail import Mail, Email, To, Content
3. To assign your API key to the SendGrid API client:
my_sg = sendgrid.SendGridAPIClient(api_key = os.environ.get('SENDGRID_API_KEY'))
4. To send an email, create the body and generate JSON representation. Refer to SendGrid’s complete code block:
import sendgrid
import os
from sendgrid.helpers.mail import Mail, Email, To, Content
my_sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
# Change to your verified sender
from_email = Email("[email protected]")
# Change to your recipient
to_email = To("[email protected]")
subject = "Lorem ipsum dolor sit amet"
content = Content("text/plain", "consectetur adipiscing elit")
mail = Mail(from_email, to_email, subject, content)
# Get a JSON-ready representation of the Mail object
mail_json = mail.get()
# Send an HTTP POST request to /mail/send
response = my_sg.client.mail.send.post(request_body=mail_json)
1. and navigate to the Designer tab on the left.
2. Click "Create Notification” Now you’re ready to integrate a provider for email. Courier supports direct integrations with several popular email providers such as SendGrid, Postmark, MailChimp Transactional, and more. For this tutorial, let’s go with SendGrid.
3. To integrate with the channel of your choice, click "+ Add Channel” Once you’ve added your configured SendGrid integration, you can start adding content.
5. With your Courier account configured, create a Python script. You can download via pip install trycourier
.
from trycourier import Courier
client = Courier(auth_token="Courier_Authentication_Token")
response = client.send(
event="your-notification-id" #Your notification ID from Courier
recipient="your-recipient-id" #Usually your system's User ID
profile={
"email": "[email protected]" #The recipient’s email address
},
data={
"Loredm Ipsum": "dolor sit amet" #Tthe message you wish to send
}
)
print(response['messageId'])
from trycourier import Courier
client = Courier(auth_token="Courier_Authentication_Token")
response = client.send(
event="your-event-id",
recipient="your-recipient-id",
profile={
"email": "recipient_id",
"phone_number": "recipient_number"
},
data={
"Loredm Ipsum": "dolor sit amet"
},
override={} #Pass the override here
)
print(response['messageId'])
"override": {
“channel”: {
“email”: {
"attachments": [
{
"filename": "sample_file.txt",
"contentType": "text/plain",
"data": "SGk="
}
]
}
}
}