How To Format Email to Send as SMS

时间:2017-04-10 00:52:10

标签: python python-3.x sms mime mime-message

I want to be notify people via SMS when certain things happen. Seems like it should be pretty straighforward. But when the SMS arrives it has the sender and subject line in the message, and I can't figure out how to adjust the message to get rid of it.

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

message = MIMEMultipart()
message['From'] = "xyz@gmail.com"
message['To'] = "5551234567@tmomail.net"
message['Subject'] = "FOOBAR!"

text = "Hello, world!"
message.attach(MIMEText(text.encode("utf-8"), "plain", "utf-8"))

server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(message["From"], "SuperSecretString")

server.sendmail(message["From"], [message["To"]], text)

Produces something like:

xyz@gmail.com / FOOBAR!/ Hello, world!, and all I want to see is Hello, world!

1 个答案:

答案 0 :(得分:3)

经过一些研究后,似乎使用短信网关发送短信是有限的,因为你只能控制你的短信格式。

然而,对正在发送的文本结构的这种修改在Sprint上以你想要的格式为我工作:

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

message = MIMEMultipart()
message['From'] = "myemail@pensivepost.com"
message['To'] = "1234567@messaging.sprintpcs.com"
message['Subject'] = "FOOBAR!"

text = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
       % (message['From'], ", ".join(message['To']), message['Subject']) )
text += "Hello World!\r\n"

message.attach(MIMEText(text.encode("utf-8"), "plain", "utf-8"))

server = smtplib.SMTP("smtp.zoho.com", 587)
server.starttls()
server.login(message["From"], "**********")

server.sendmail(message["From"], [message["To"]], text)

请注意,我从this帖子中获取了此邮件正文格式,并根据您的情况进行了调整。