是否可以通过Linux命令行发送SMS消息?

时间:2019-06-09 03:51:28

标签: command-line sms messaging

我需要通过Linux命令行将短信发送到特定的电话号码。我已经搜索了一种方法,但是大多数方法已经过时,或者看起来像是骗局。

还是有这种可能的,如果可以,那么最好/最便宜的方法是什么?

1 个答案:

答案 0 :(得分:3)

您可以通过在Linux命令行中运行Python脚本来发送SMS。

我在此处包括了脚本的python代码。

import smtplib 
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

email = "Your Email"
pas = "Your Pass"

sms_gateway = 'number@tmomail.net'
# The server we use to send emails in our case it will be gmail but every email provider has a different smtp 
# and port is also provided by the email provider.
smtp = "smtp.gmail.com" 
port = 587
# This will start our email server
server = smtplib.SMTP(smtp,port)
# Starting the server
server.starttls()
# Now we need to login
server.login(email,pas)

# Now we use the MIME module to structure our message.
msg = MIMEMultipart()
msg['From'] = email
msg['To'] = sms_gateway
# Make sure you add a new line in the subject
msg['Subject'] = "You can insert anything\n"
# Make sure you also add new lines to your body
body = "You can insert message here\n"
# and then attach that body furthermore you can also send html content.
msg.attach(MIMEText(body, 'plain'))

sms = msg.as_string()

server.sendmail(email,sms_gateway,sms)

# lastly quit the server
server.quit()

但是为此,您需要运营商的SMS网关。

有关详情,请看:Link