14

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!

Batman
  • 8,571
  • 7
  • 41
  • 80
  • 3
    This really depends on what format your email to SMS gateway supports, there is no standard mapping defined between e-mail and SMS. I would personally recommend abandoning email-to-SMS gateways and use something like Twilio who offer easier and more consistent JSON APIs to send SMS. – André Borie Apr 13 '17 at 14:19
  • 1
    Yeah, I ended up using Twilio, but it's the principle of the thing now. – Batman Apr 14 '17 at 16:22

1 Answers1

3

After doing a bit of research, it seems that using SMS gateways to send SMS messages is limiting in that you only so much control over the format of your text.

However this modification on the structure of the text being sent works for me on Sprint in the format you want:

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)

Note that I took this message body format from this thread and adapted it to your case.

Community
  • 1
  • 1
dylan-slack
  • 102
  • 5