Skip to content

Instantly share code, notes, and snippets.

@Alexhuszagh
Last active October 12, 2019 03:46
Show Gist options
  • Save Alexhuszagh/cbfede3a7200778896f7a9d456d65f20 to your computer and use it in GitHub Desktop.
Save Alexhuszagh/cbfede3a7200778896f7a9d456d65f20 to your computer and use it in GitHub Desktop.
Programmatically Send Text to Cell Phone
#!/usr/bin/env python
'''
send_text
=========
Send text with a message.
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
'''
# An example of using the script:
# python3 send_text.py -p=verizon -n=NXXNXXNNN -s='Subject' -b='Message'
# GMAIL ONLY
# ----------
# A few notes: This uses less-secured apps by default for GMail, so I would
# not recommend using this script from an email account you care about.
#
# To enable less-secured apps with GMail, go to the following link:
# https://myaccount.google.com/lesssecureapps?utm_source=google-account&utm_medium=web
# Please note that it may take a few minutes for these changes to register.
#
# If you would like to use this script with GMail on a secured account,
# please use the GMail API.
# To install the GMail API, run:
# pip install --upgrade google-api-python-client
# Next, substitute the logic below with code adapted from here:
# https://medium.com/lyfepedia/sending-emails-with-gmail-api-and-python-49474e32c81f
import argparse
import smtplib
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--provider', required=True)
parser.add_argument('-n', '--number', required=True)
parser.add_argument('-s', '--subject', required=True)
parser.add_argument('-b', '--body', required=True)
EMAIL_HOST = '' # 'smtp.gmail.com' for gmail
EMAIL_PORT = 0 # 465 for gmail
EMAIL_USERNAME = '' # Username for email account
EMAIL_PASSWORD = '' # Password for email account
EMAIL_TEMPLATE = """\
From: {send_from}
To: {send_to}
Subject: {subject}
{body}
"""
# Add more providers if yours isn't here.
PROVIDERS = {
'att': '@txt.att.net',
'boost': '@smsmyboostmobile.com',
'cricket': '@sms.cricketwireless.net',
'sprint': '@messaging.sprintpcs.com',
'tmobile': '@tmomail.net',
'uscellular': '@email.uscc.net',
'verizon': '@vtext.com',
'virgin': '@vmobl.com',
}
def format_email(send_to, send_from, subject, body):
'''Format the email body.'''
return EMAIL_TEMPLATE.format(
send_to=send_to,
send_from=send_from,
subject=subject,
body=body
)
def send_email(send_to, send_from, subject, body):
'''Send email to recipient.'''
text = format_email(send_to, send_from, subject, body)
try:
smtp = smtplib.SMTP_SSL(EMAIL_HOST, EMAIL_PORT)
smtp.login(EMAIL_USERNAME, EMAIL_PASSWORD)
smtp.sendmail(send_from, send_to, text)
finally:
smtp.close()
def main():
args = parser.parse_args()
send_to = f'{args.number}{PROVIDERS[args.provider]}'
send_from = EMAIL_USERNAME
send_email(send_to, send_from, args.subject, args.body)
if __name__ == '__main__':
main()
@Alexhuszagh
Copy link
Author

Perhaps license it?

Done, it's fairly simple code, so I've made it public domain (or as much as I possibly can).

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