Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Format Email to Send as SMS

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'] = "[email protected]"
message['To'] = "[email protected]"
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:

[email protected] / FOOBAR!/ Hello, world!, and all I want to see is Hello, world!

like image 590
Batman Avatar asked Apr 10 '17 00:04

Batman


People also ask

Can I send Gmail as SMS?

Open Gmail and click on Compose from the Main Menu. 2. In the To field, enter the recipient's 10-digit cell phone number (no country code) followed by “@” and the SMS gateway address textmagic.com. For example, this is how you would send a message to [email protected].

Can SMTP be used to send SMS?

The SMTP (E-mail to SMS) connection can be used to send SMS messages through E-mail to SMS services. E-mail to SMS connectivity is provided by many mobile network operators and multichannel IP based GSM gateways. If you install this connection, you can connect to these services.


1 Answers

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'] = "[email protected]"
message['To'] = "[email protected]"
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.

like image 184
dylan-slack Avatar answered Nov 07 '22 15:11

dylan-slack