本文共 1672 字,大约阅读时间需要 5 分钟。
引言
作为一名致力于Linux和Python技术的学习者,掌握基础知识远远不够,真正的挑战在于将技术应用于实际问题的解决。本文将分享四种实用Python和Linux运维脚本,帮助您轻松实现短信和邮件的秒发功能。环境要求
实战案例
用Python发送短信from twilio.rest import Clientaccount_sid = "your_account_sid"auth_token = "your_auth_token"client = Client(account_sid, auth_token)message = client.messages.create( to="+861xxxxxxxxxx", from_="+1xxxxxxxxxx", body="测试消息,请勿回复!")print(f"短信已发送,SID: {message.sid}")
用Linux发送短信
#!/bin/bashcurl -X POST -d "apikey=your_apikey&mobile=手机号码&text=测试消息,请勿回复" https://sms-api.luosimao.com/v1/send.json
用Python发送带附件的邮件
import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.application import MIMEApplicationsender = 'sender@example.com'receiver = 'receiver@example.com'subject = '邮件主题'attachment_path = '/path/to/attachment.pdf'message = MIMEMultipart()message['From'] = sendermessage['To'] = receivermessage['Subject'] = subjectbody = MIMEText('这是邮件的正文内容')message.attach(body)with open(attachment_path, 'rb') as attachment: attachment_part = MIMEApplication(attachment.read()) attachment_part.add_header('Content-Disposition', 'attachment', filename='attachment.pdf') message.attach(attachment_part)server = smtplib.SMTP('smtp.server.com', 587)server.login('username', 'password')server.sendmail(sender, receiver, message.as_string())
用Linux发送带附件的邮件
#!/bin/bashecho "这是邮件的正文内容" | mail -s "邮件主题" -a /path/to/attachment.pdf user@example.com
总结
本文分享了四种实用Python和Linux运维脚本,涵盖了短信和邮件的秒发功能。通过Twilio API、curl命令以及Python的smtplib和email库,您可以轻松实现这些功能。这些工具和脚本能够帮助您提高工作效率,解决运维过程中的通信需求。记得关注【运维家】公众号,获取更多关于Linux和Python的实用技术文章!
转载地址:http://tlzfk.baihongyu.com/