Sending Text Messages From An E-Mail With Python

Published on 09/19/19 at 01:14 EST by Max Bridgland


This is the method I use for my 2FA to not have to use Twilio or anything else. This is free except for the fact I'm using an enterprise domain email hosted on G-Mail.

Here is my send_sms function:

import smtplib
from email.message import EmailMessage
from app import app

mail_settings = {
    "MAIL_SERVER": 'smtp.gmail.com',
    "MAIL_PORT": 465,
    "MAIL_USE_TLS": False,
    "MAIL_USE_SSL": True,
    "MAIL_USERNAME": '[email protected]',
    "MAIL_PASSWORD": 'password-here',
    "MAIL_END": '@tmomail.net' # TMobile Email
}

def send(message):
        # Replace the number with your own, or consider using an argument\dict for multiple people.
    if app.testing:
        to_number = '555-555-0000{}'.format(mail_settings['MAIL_END'])
    else: # pragma: no cover
        to_number = 'XXX-XXX-XXXX{}'.format(mail_settings['MAIL_END'])
    auth = (mail_settings['MAIL_USERNAME'], mail_settings['MAIL_PASSWORD'])

    # Establish a secure session with gmail's outgoing SMTP server using your gmail account
    server = smtplib.SMTP( "smtp.gmail.com", 587 )
    server.starttls()
    server.login(auth[0], auth[1])
    msg = EmailMessage()
    msg.set_content(message)
    msg['Subject'] = " "
    msg['From'] = "Me!"
    msg['To'] = to_number
    server.send_message(msg)
    # Send text message through SMS gateway of destination number


Comments:

How about a PSG tutorial on making a GUI to send and receive text messages... or send text messages? No one has done that yet.

Commented on 09/20/19 at 14:48 EST by PSG