首页 > 解决方案 > 是否可以通过 Linux 命令行发送 SMS 消息?

问题描述

我需要通过 Linux 命令行向特定的电话号码发送短信。我已经寻找一种方法来做到这一点,但大多数都已经过时,或者看起来像是骗局。

这样的事情仍然可能吗?如果是这样,最好/最便宜的方法是什么?

标签: command-linesmsmessaging

解决方案


您可以通过在 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 网关。

详情请看:链接


推荐阅读