Skip to content

Instantly share code, notes, and snippets.

@rotated8
Last active March 1, 2022 08:29
Show Gist options
  • Save rotated8/4746555 to your computer and use it in GitHub Desktop.
Save rotated8/4746555 to your computer and use it in GitHub Desktop.
Send emails through Gmail with Python. Send text messages using the SMS gateways. If you use Gmail's two-factor authentication, you'll need to set up an application specific password.
gmail_username = 'username@gmail.com'
recipients = 'recipient@example.com'
# https://en.wikipedia.org/wiki/List_of_SMS_gateways
# 10DigitNumber@text.att.net for AT&T
# 10DigitNumber@vtext.com for Verizon
from email.mime.text import MIMEText
msg = MIMEText('This is the body of the message.')
msg['Subject'] = 'Blank subject lines look like spam.'
msg['From'] = gmail_username
msg['To'] = recipients
msg = msg.as_string()
import smtplib
session = smtplib.SMTP('smtp.gmail.com', 587)
session.ehlo()
session.starttls()
session.login(gmail_username, 'your-gmail-or-application-specific-password')
session.sendmail(gmail_username, recipients, msg)
session.quit()
@testfiles
Copy link

for tmobile, it is 10DigitNum@tmomail.net

@robertcatgithub
Copy link

server.sendmail(gmail_username, recipients, msg)

NameError: name 'server' is not defined

@rotated8
Copy link
Author

rotated8 commented Jun 8, 2015

Whoops, should be 'session'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment