发送短信
providers.pyPROVIDERS = { "AT&T": {"sms": "txt.att.net","mms": "mms.att.net", "mms_support": True}, "Boost Mobile": { "sms": "sms.myboostmobile.com", "mms": "myboostmobile.com", "mms_support": True, }, "C-Spire": {"sms": "cspire1.com","mms_support": False}, "Cricket Wireless": { "sms":"sms.cricketwireless.net ", "mms": "mms.cricketwireless.net", "mms_support": True, }, "Consumer Cellular": {"sms":"mailmymobile.net", "mms_support": False}, "Google Project Fi": {"sms":"msg.fi.google.com", "mms_support": True}, "Metro PCS": {"sms": "mymetropcs.com","mms_support": True}, "Mint Mobile": {"sms": "mailmymobile.net","mms_support": False}, "Page Plus": { "sms": "vtext.com", "mms": "mypixmessages.com", "mms_support": True, }, "Republic Wireless": { "sms": "text.republicwireless.com", "mms_support": False, }, "Sprint": { "sms": "messaging.sprintpcs.com", "mms": "pm.sprint.com", "mms_support": True, }, "Straight Talk": { "sms": "vtext.com", "mms": "mypixmessages.com", "mms_support": True, }, "T-Mobile": {"sms": "tmomail.net","mms_support": True}, "Ting": {"sms": "message.ting.com","mms_support": False}, "Tracfone": {"sms": "", "mms":"mmst5.tracfone.com", "mms_support": True}, "U.S. Cellular": { "sms": "email.uscc.net", "mms": "mms.uscc.net", "mms_support": True, }, "Verizon": {"sms": "vtext.com","mms": "vzwpix.com", "mms_support": True}, "Virgin Mobile": { "sms": "vmobl.com", "mms": "vmpix.com", "mms_support": True, }, "Xfinity Mobile": { "sms": "vtext.com", "mms": "mypixmessages.com", "mms_support": True, },}
-
email:用于格式化电子邮件。 -
smtplib:通过 SMTP 服务器发送电子邮件。 -
ssl:连接SMTP 服务器。 -
providers:上面提到的移动提供商的配置。
main.pyimport email, smtplib, sslfrom providers import PROVIDERS
send_sms_via_email( number: str, message: str, provider: str, sender_credentials: tuple, subject: str = "sent using etext", smtp_server: str = "smtp.gmail.com", smtp_port: int = 465,):
-
number:字符串类型,发送电子邮件的电话号码。 -
message:字符串类型,实际的消息。 -
provider:字符串类型,移动提供商。 -
sender_credentials:元组类型,发件人凭据,不仅包括发送电子邮件,而且还包括上述我们通过 Google 应用获得的密码。 -
subject:字符串类型,消息主题。注意,有些 SMS 网关不允许发送格式不正确的电子邮件,因此我们在此加入了主题。 -
smtp_server:字符串类型,SMTP服务器。 -
smtp_port:整数类型,SMTP服务器端口。
str, message: str, provider: str, sender_credentials: tuple, subject: str = "sent using etext", smtp_server: str = "smtp.gmail.com", smtp_port: int = 465,): sender_email, email_password = sender_credentials :
send_sms_via_email( number: str, message: str, provider: str, sender_credentials: tuple, subject: str = "sent using etext", smtp_server: str = "smtp.gmail.com", smtp_port: int = 465,): sender_email, email_password = sender_credentials receiver_email =f'{number}@{PROVIDERS.get(provider).get("sms")}'
main.pydef send_sms_via_email( number: str, message: str, provider: str, sender_credentials: tuple, subject: str = "sent using etext", smtp_server: str = "smtp.gmail.com", smtp_port: int = 465,): sender_email, email_password = sender_credentials receiver_email =f'{number}@{PROVIDERS.get(provider).get("sms")}' email_message =f"Subject:{subject}\nTo:{receiver_email}\n{message}"
main.pydef send_sms_via_email( number: str, message: str, provider: str, sender_credentials: tuple, subject: str = "sent using etext", smtp_server: str = "smtp.gmail.com", smtp_port: int = 465,): sender_email, email_password = sender_credentials receiver_email =f'{number}@{PROVIDERS.get(provider).get("sms")}' email_message =f"Subject:{subject}\nTo:{receiver_email}\n{message}" with smtplib.SMTP_SSL( smtp_server, smtp_port, context=ssl.create_default_context() ) as email: email.login(sender_email, email_password) email.sendmail(sender_email, receiver_email, email_message)
main.pydef main(): number = "5623720883" message = "hello world!" provider = "T-Mobile" sender_credentials = ("[email protected]","password") send_sms_via_email(number, message, provider, sender_credentials)
if __name__ == "__main__": main()
发送 MMS 消息
send_mms_via_email( number: str, message: str, provider: str, sender_credentials: tuple, subject: str = "sent using etext", smtp_server: str = "smtp.gmail.com", smtp_port: int = 465,): sender_email, email_password = sender_credentials receiver_email =f'{number}@{PROVIDERS.get(provider).get("sms")}'
为了发送 MMS 消息,我们还需要添加三个参数:
-
file_path:由于我们需要一个文件来发送多媒体消息,因此需要再添加一个参数用以表示文件的路径。 -
mime_maintype:多媒体文件的主类型。 -
mime_subtype:多媒体文件的子类型。
send_mms_via_email( number: str, message: str, file_path: str, mime_maintype: str, mime_subtype: str, provider: str, sender_credentials: tuple, subject: str = "sent using etext", smtp_server: str = "smtp.gmail.com", smtp_port: int = 465,):
此外,我们还需要导入发送 MMS 的工具函数:
main.pyfrom email import encodersfrom email.mime.base import MIMEBasefrom email.mime.multipart importMIMEMultipartfrom email.mime.text import MIMETextfrom os.path import basename
接下来,我们来写发送电子邮件的函数。我们发送的邮件不需要显示主题、收件人和消息,但邮件应该包含多个部分:第一部分,消息本身;第二部分,电子邮件的附件。
send_mms_via_email( number: str, message: str, file_path: str, mime_maintype: str, mime_subtype: str, provider: str, sender_credentials: tuple, subject: str = "sent using etext", smtp_server: str = "smtp.gmail.com" smtp_port: int = 465,): sender_email, email_password = sender_credentials receiver_email =f'{number}@{PROVIDERS.get(provider).get("sms")}' email_message = MIMEMultipart() email_message["Subject"] = subject email_message["From"] = sender_email email_message["To"] = receiver_email
接下来,我们在邮件中添加附件——普通类型(纯文本)。
send_mms_via_email( number: str, message: str, file_path: str, mime_maintype: str, mime_subtype: str, provider: str, sender_credentials: tuple, subject: str = "sent using etext", smtp_server: str = "smtp.gmail.com", smtp_port: int = 465,): sender_email, email_password = sender_credentials receiver_email =f'{number}@{PROVIDERS.get(provider).get("sms")}' email_message = MIMEMultipart() email_message["Subject"] = subject email_message["From"] = sender_email email_message["To"] = receiver_email email_message.attach(MIMEText(message, "plain"))
MIME类型
附加文件
main.pydef send_mms_via_email( number: str, message: str, file_path: str, mime_maintype: str, mime_subtype: str, provider: str, sender_credentials: tuple, subject: str = "sent using etext", smtp_server: str = "smtp.gmail.com", smtp_port: int = 465,):sender_email, email_password = sender_credentials receiver_email =f'{number}@{PROVIDERS.get(provider).get("sms")}' email_message = MIMEMultipart() email_message["Subject"] = subject email_message["From"] = sender_email email_message["To"] = receiver_email email_message.attach(MIMEText(message, "plain")) with open(file_path, "rb") as attachment: part = MIMEBase(mime_maintype, mime_subtype) part.set_payload(attachment.read()) encoders.encode_base64(part) part.add_header( "Content-Disposition", f"attachment;filename={basename(file_path)}", ) email_message.attach(part)
最后,我们不能直接发送 MIME 类型的电子邮件,我们必须在上下文管理器之外将其转换为文本。
main.pydef send_mms_via_email( number: str, message: str, file_path: str, mime_maintype: str, mime_subtype: str, provider: str, sender_credentials: tuple, subject: str = "sent using etext", smtp_server: str = "smtp.gmail.com", smtp_port: int = 465,): sender_email, email_password = sender_credentials receiver_email =f'{number}@{PROVIDERS.get(provider).get("sms")}' email_message = MIMEMultipart() email_message["Subject"] = subject email_message["From"] = sender_email email_message["To"] = receiver_email email_message.attach(MIMEText(message, "plain")) with open(file_path, "rb") as attachment: part = MIMEBase(mime_maintype, mime_subtype) part.set_payload(attachment.read()) encoders.encode_base64(part) part.add_header( "Content-Disposition", f"attachment;filename={basename(file_path)}", ) email_message.attach(part) text = email_message.as_string()
main.pydef send_mms_via_email( number: str, message: str, file_path: str, mime_maintype: str, mime_subtype: str, provider: str, sender_credentials: tuple, subject: str = "sent using etext", smtp_server: str = "smtp.gmail.com", smtp_port: int = 465,): sender_email, email_password = sender_credentials receiver_email =f'{number}@{PROVIDERS.get(provider).get("sms")}' email_message = MIMEMultipart() email_message["Subject"] = subject email_message["From"] = sender_email email_message["To"] = receiver_email email_message.attach(MIMEText(message, "plain")) with open(file_path, "rb") as attachment: part = MIMEBase(mime_maintype, mime_subtype) part.set_payload(attachment.read()) encoders.encode_base64(part) part.add_header( "Content-Disposition", f"attachment;filename={basename(file_path)}", ) email_message.attach(part) text = email_message.as_string() with smtplib.SMTP_SSL( smtp_server, smtp_port, context=ssl.create_default_context() ) as email: email.login(sender_email, email_password) email.sendmail(sender_email, receiver_email, text)
下面,我们来创建 main 函数。与 SMS 的代码类似,我们只需添加文件路径和 MIME 类型。为了发送一个 png 图像,我们必须指定 mime_maintype 为 “image”,mime_subtype 为 “png”。
main.pydef main(): file_path = "/path/to/file/file.png" mime_maintype = "image" mime_subtype = "png" number = "5623720883" message = "hello world!" provider = "T-Mobile" sender_credentials = ("[email protected]","password") send_mms_via_email( number, message, file_path, mime_maintype, mime_subtype, provider, sender_credentials, )if __name__ == "__main__": main()
main.pyimport email, smtplib, sslfrom providers import PROVIDERS# used for MMSfrom email import encodersfrom email.mime.base import MIMEBasefrom email.mime.multipart importMIMEMultipartfrom email.mime.text import MIMETextfrom os.path import basenamedef send_sms_via_email( number: str, message: str, provider: str, sender_credentials: tuple, subject: str = "sent using etext", smtp_server: str = "smtp.gmail.com", smtp_port: int = 465,): sender_email, email_password = sender_credentials receiver_email =f'{number}@{PROVIDERS.get(provider).get("sms")}' email_message =f"Subject:{subject}\nTo:{receiver_email}\n{message}" with smtplib.SMTP_SSL( smtp_server, smtp_port, context=ssl.create_default_context() ) as email: email.login(sender_email, email_password) email.sendmail(sender_email, receiver_email, email_message)def send_mms_via_email( number: str, message: str, file_path: str, mime_maintype: str, mime_subtype: str, provider: str, sender_credentials: tuple, subject: str = "sent using etext", smtp_server: str = "smtp.gmail.com", smtp_port: int = 465,): sender_email, email_password = sender_credentials receiver_email =f'{number}@{PROVIDERS.get(provider).get("sms")}' email_message=MIMEMultipart() email_message["Subject"] = subject email_message["From"] = sender_email email_message["To"] = receiver_email email_message.attach(MIMEText(message, "plain")) with open(file_path, "rb") as attachment: part = MIMEBase(mime_maintype, mime_subtype) part.set_payload(attachment.read()) encoders.encode_base64(part) part.add_header( "Content-Disposition", f"attachment;filename={basename(file_path)}", ) email_message.attach(part) text = email_message.as_string() with smtplib.SMTP_SSL( smtp_server, smtp_port, context=ssl.create_default_context() ) as email: email.login(sender_email, email_password) email.sendmail(sender_email, receiver_email, text)def main(): number = "5623720883" message = "hello world!" provider = "T-Mobile" sender_credentials = ("[email protected]","password") # SMS send_sms_via_email(number, message, provider, sender_credentials) # MMS file_path = "/path/to/file/file.png" mime_maintype = "image" mime_subtype = "png" send_mms_via_email( number, message, file_path, mime_maintype, mime_subtype, provider, sender_credentials, ) if __name__ == "__main__": main()
常见的 MIME 类型
《新程序员003》正式上市,50余位技术专家共同创作,云原生和数字化的开发者们的一本技术精选图书。内容既有发展趋势及方法论结构,华为、阿里、字节跳动、网易、快手、微软、亚马逊、英特尔、西门子、施耐德等30多家知名公司云原生和数字化一手实战经验!
☞IntelliJ 平台彻底停用 Log4j 组件!
文章评论