-
Notifications
You must be signed in to change notification settings - Fork 17
/
send_txt_msg.py
77 lines (63 loc) · 2.32 KB
/
send_txt_msg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# *- coding: utf-8 -*-
# send_txt_msg.py
# 04-02-2021 03:08:34 EDT
# (c) 2021 acamso
"""Sends TXT message with GMail.
This is a demonstration on how to send an text message with Python.
In this example, we use GMail to send the SMS message,
but any host can work with the correct SMTP settings.
Each carrier has a unique SMS gateway hostname.
This method is completely free and can be useful in a variety of ways.
Video: https://youtu.be/hKxtMaa2hwQ
Turn on: https://myaccount.google.com/lesssecureapps
"""
import asyncio
import re
from email.message import EmailMessage
from typing import Collection, List, Tuple, Union
import aiosmtplib
HOST = "smtp.gmail.com"
# https://kb.sandisk.com/app/answers/detail/a_id/17056/~/list-of-mobile-carrier-gateway-addresses
# https://www.gmass.co/blog/send-text-from-gmail/
CARRIER_MAP = {
"verizon": "vtext.com",
"tmobile": "tmomail.net",
"sprint": "messaging.sprintpcs.com",
"at&t": "txt.att.net",
"boost": "smsmyboostmobile.com",
"cricket": "sms.cricketwireless.net",
"uscellular": "email.uscc.net",
}
# pylint: disable=too-many-arguments
async def send_txt(
num: Union[str, int], carrier: str, email: str, pword: str, msg: str, subj: str
) -> Tuple[dict, str]:
to_email = CARRIER_MAP[carrier]
# build message
message = EmailMessage()
message["From"] = email
message["To"] = f"{num}@{to_email}"
message["Subject"] = subj
message.set_content(msg)
# send
send_kws = dict(username=email, password=pword, hostname=HOST, port=587, start_tls=True)
res = await aiosmtplib.send(message, **send_kws) # type: ignore
msg = "failed" if not re.search(r"\sOK\s", res[1]) else "succeeded"
print(msg)
return res
async def send_txts(
nums: Collection[Union[str, int]], carrier: str, email: str, pword: str, msg: str, subj: str
) -> List[Tuple[dict, str]]:
tasks = [send_txt(n, carrier, email, pword, msg, subj) for n in set(nums)]
return await asyncio.gather(*tasks)
if __name__ == "__main__":
_num = "999999999"
_carrier = "verizon"
_email = "user@gmail.com"
_pword = "pword"
_msg = "Dummy msg"
_subj = "Dummy subj"
coro = send_txt(_num, _carrier, _email, _pword, _msg, _subj)
# _nums = {"999999999", "000000000"}
# coro = send_txts(_nums, _carrier, _email, _pword, _msg, _subj)
asyncio.run(coro)