Created
October 3, 2020 20:59
-
-
Save brayvasq/db081200debc91e1eb886a79edb1ebfe to your computer and use it in GitHub Desktop.
Send emails with Python and Gmail SMTP server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# main.py | |
# Import section .... | |
import os | |
import sys | |
from dotenv import load_dotenv | |
from smtplib import SMTP, SMTPException # Import SMTP module | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from email.mime.base import MIMEBase | |
import email.encoders | |
# Loading ENV variables | |
load_dotenv() | |
# Constants section .... | |
# GMAIL account sender | |
SENDER = os.getenv('SENDER') | |
PASSWORD = os.getenv('PASSWORD') | |
# Accounts receivers | |
RECEIVERS = os.getenv('RECEIVERS').split(',') | |
# SMTP server options | |
HOST = os.getenv('HOST') | |
PORT = os.getenv('PORT') | |
# .... | |
# SMTP object | |
SMTP_OBJECT = SMTP(host=HOST, port=PORT) | |
# Methods section .... | |
# Base function | |
def send_email(message=""): | |
""" | |
Send an email using the credentials defined before | |
Parameters | |
---------- | |
message : str | |
content to send in the message | |
""" | |
try: | |
SMTP_OBJECT.ehlo() | |
SMTP_OBJECT.starttls() | |
SMTP_OBJECT.login(SENDER, PASSWORD) | |
SMTP_OBJECT.sendmail(SENDER, RECEIVERS, message) | |
SMTP_OBJECT.quit() | |
print("Successfully sent email") | |
except SMTPException: | |
print ("Error: unable to send email") | |
def basic_email(message = ""): | |
""" | |
Basic email with plain text message | |
""" | |
send_email(message) | |
def send_from_txt(): | |
""" | |
Send an email reading the content message from a txt file | |
""" | |
message = MIMEMultipart('alternative') | |
message['Subject'] = 'TXT file content' | |
message['From'] = SENDER | |
body = "Email message body" | |
content = MIMEText(body, 'plain') | |
message.attach(content) | |
# process text file | |
file_open = open('text.txt', 'rb') | |
file_content = MIMEText(_text=file_open.read(), _charset='utf-8') | |
file_open.close() | |
message.attach(file_content) | |
# Send email | |
send_email(message.as_string()) | |
def send_attachment(): | |
""" | |
Send an email with an attachment | |
""" | |
message = MIMEMultipart('alternative') | |
message['Subject'] = 'Attachment' | |
message['From'] = SENDER | |
attachment = MIMEBase('application', 'octet-stream') | |
attachment.set_payload(open('file.pdf', 'rb').read()) | |
attachment.add_header('Content-Disposition', 'attachment; filename="file.pdf"') | |
email.encoders.encode_base64(attachment) | |
message.attach(attachment) | |
# Send email | |
send_email(message.as_string()) | |
# Main section ... | |
if __name__ == "__main__": | |
# Message to send | |
message = """ | |
this message is sent from python for testing purposes | |
""" | |
if len(sys.argv)> 1 : | |
if sys.argv[1] == '--help': | |
print('Info: ') | |
print('--help List the options to send an email') | |
print('--basic Sends a plain text email') | |
print('--txt Send an email from a text file content') | |
print('--attach Send an email with an attachment') | |
elif sys.argv[1] == '--basic': | |
print("Sending a plain text email") | |
basic_email(message) | |
elif sys.argv[1] == '--txt': | |
print("Sending an email from a text file content") | |
send_from_txt() | |
elif sys.argv[1] == '--attach': | |
print("Sending an email with an attachment") | |
send_attachment() | |
else: | |
print("Please give the type of message to send.") | |
print("For help execute `python main.py --help`") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment