Skip to content

Instantly share code, notes, and snippets.

@rodgeraraujo
Last active August 23, 2021 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodgeraraujo/b8c132cad969d6768030b8c591ab78c5 to your computer and use it in GitHub Desktop.
Save rodgeraraujo/b8c132cad969d6768030b8c591ab78c5 to your computer and use it in GitHub Desktop.
Simple python script that sends Gmail emails!
import os
import smtplib, ssl
from email.mime.text import MIMEText
import os
def get_credentials():
"""
Get the credentials from the environment variables.
"""
return {
"username": os.environ["GMAIL_USERNAME"],
"password": os.environ["GMAIL_PASSWORD"],
}
def create_server():
"""
Create a server to send Gmail emails.
"""
credentials = get_credentials()
port = 465 # For SSL
smtp_server = "smtp.gmail.com"
sender_email = credentials["username"]
password = credentials["password"]
context = ssl.create_default_context()
server = smtplib.SMTP_SSL(smtp_server, port, context=context)
server.login(sender_email, password)
return server
def create_email_message(sender, to_receivers, subject, body):
"""
Create an email message.
Arguments:
to_email -- the email address to send to
subject -- the subject of the email
body -- the body of the email
"""
message = MIMEText(body, "html")
message["Subject"] = subject
message["From"] = sender
message["To"] = ",".join(to_receivers)
return message
def send_with_attachment(to_receivers, subject, body, attachment_path):
"""
Send an email with an attachment.
Arguments:
to_email -- the email address to send to
subject -- the subject of the email
body -- the body of the email
attachment_path -- the path to the attachment
"""
credentials = get_credentials()
sender_email = credentials["username"]
message = create_email_message(sender_email, to_receivers, subject, body)
with open(attachment_path, "rb") as file:
attachment = MIMEText(file.read())
attachment.add_header(
"Content-Disposition",
"attachment",
filename=os.path.basename(attachment_path),
)
message.attach(attachment)
server = create_server()
server.sendmail(sender_email, to_receivers, message.as_string())
server.close()
def send_email(to_receivers, subject, body):
"""
Send an email.
Arguments:
to_email -- the email address to send to
subject -- the subject of the email
body -- the body of the email
"""
credentials = get_credentials()
sender_email = credentials["username"]
message = create_email_message(sender_email, to_receivers, subject, body)
server = create_server()
server.sendmail(sender_email, to_receivers, message.as_string())
server.close()
receivers = ["test@gmail.com"]
subject = "Subject line goes here"
body = "Test send email with gmail Account"
attachment_path = "email_attachment.txt"
send_email(receivers, subject, body)
# send_with_attachment(receivers, subject, body, attachment_path)
print("Email sent!")
# To run:
# export GMAIL_USERNAME="gmail_email"
# export GMAIL_PASSWORD="email_password"
# python3 sender_email.py
# This will display a message 'Email sent!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment