18
loading...
This website collects cookies to deliver better user experience
smtplib
module to send email using SMTP (Simple Mail Transfer Protocol), which is an application-level protocol. Note that the module makes use of RFC 821 protocol for SMTP. I’ll show you how to use Gmail’s SMTP server for this walkthrough. Set up a Gmail account for sending your emails. Since you’ll be feeding a plaintext password to the program, Google considers the SMTP connection less secure.
Go to the account settings and allow less secure apps to access the account. As an aside, Gmail doesn't necessarily use SMTP on their internal mail servers; however, Gmail SMTP is an interface enabled by Google's smtp.gmail.com server. You might find smtp.gmail.com in email clients like Thunderbird, Outlook, and others.
Import smtplib
. Since Python comes pre-packaged with smtplib
, all you have to do is create a Python file and import smtplib
into it.
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()
.
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)
.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)
.sendmail()
.To install the sendgrid
package on your machine, refer to SendGrid's GitHub installation guide or directly install via pip install sendgrid
.
To use the package in a Python script:
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'))
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)
To design your notification, just drag and drop the template blocks. You can also add custom code to your notification, either by overriding the entire email or by adding a code block. If you decide to send your notification over another channel, you can reuse the same template blocks and Courier will take care of updating the formatting.
With your Courier account configured, create a Python script. You can download Courier’s Python Package via pip install trycourier
.
Once you’ve published your notification, Courier will automatically generate a code snippet for you to use. Copy-paste the code snippet and make an API call with the following script:
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="
}
]
}
}
}