Skip to content

Instantly share code, notes, and snippets.

@brunodb3
Last active August 16, 2018 05:02
Show Gist options
  • Save brunodb3/6925c17615db026acf2070659e438995 to your computer and use it in GitHub Desktop.
Save brunodb3/6925c17615db026acf2070659e438995 to your computer and use it in GitHub Desktop.
This file is used to send an email, passing the message and subject as arguments
# -*- coding: utf-8 -*-
# this file is used to send an email, passing the message and subject as arguments
#
# to run the file:
# pyhon send-email.py "this is the subject" "this is the message"
#
# * be sure to properly config your email and password at the login function
# * this file was configured mainly for Google emails, not tested on other providers
# * for Google emails, you have to enable access for less secure apps on your account
# (see https://support.google.com/accounts/answer/6010255)
# importing external libs
import sys
import smtplib
import datetime
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# checking if params were passed
if len(sys.argv) < 3:
# terminating the script
print '=============================================================='
print ' MISSING ARGUMENTS '
print '=============================================================='
sys.exit()
# creating the current date reference
date_now = datetime.datetime.now()
string_now = str(date_now.day) + '/' + str(date_now.month) + \
', ' + str(date_now.hour) + ':{:02d}'.format(date_now.minute)
# ==============================================================================
# Functions
# send_email ===================================================================
# sends an email
def send_email(subject, message_text, message_html):
# Config your sender/receiver emails here ====================================
# sender email
sender_email = '[email protected]'
# receiver email
receivers_email = '[email protected]'
# creating the message container - the correct MIME type is
# multipart/alternative for HTML emails
message_container = MIMEMultipart('alternative')
message_container['Subject'] = subject
message_container['From'] = sender_email
message_container['To'] = receivers_email
# recording the MIME types of both parts - text/plain and text/html
part_1 = MIMEText(message_text, 'plain')
part_2 = MIMEText(message_html, 'html')
# attaching parts into message container
message_container.attach(part_1)
message_container.attach(part_2)
# creating the SMTP reference for Google servers
server = smtplib.SMTP('smtp.gmail.com:587')
# identifies, then encrypts and identifies again
server.ehlo()
server.starttls()
server.ehlo()
# Config your email login here ===============================================
# logs into Google servers
server.login('[email protected]', 'YOUR_PASSWORD_HERE')
# sends mail
server.sendmail(sender_email, receivers_email, message_container.as_string())
# closes connection to the server
server.quit()
print '=============================================================='
print ' EMAIL WAS SENT '
print '=============================================================='
# send_email ===================================================================
# End Functions
# ==============================================================================
# creating the email message and subject
subject = 'Subject: ' + sys.argv[1]
# message in text format
message_text = """Hello!
\n
\n Message sent at "%s":
\n
\n "%s"
\n
\n Your Python Script
""" % (string_now, sys.argv[2])
# message in html format
message_html = """<b>Hello!</b>
<br /><br />
Message sent at "%s":
<br /><br />
"%s"
<br /><br />
Your Python Script
""" % (string_now, sys.argv[2])
# sending the email
send_email(subject, message_text, message_html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment