Question

I can make smtplib send to other email addresses, but for some reason it is not delivering to my phone.

import smtplib
msg = 'test'
server = smtplib.SMTP('smtp.gmail.com',587)  
server.starttls()  
server.login("<username>","<password>")  
server.sendmail(username, "<number>@vtext.com", msg)  
server.quit()

The message sends successfully when the address is a gmail account, and sending a message to the phone using the native gmail interface works perfectly. What is different with SMS message numbers?

Note: using set_debuglevel() I can tell that smtplib believes the message to be successful, so I am fairly confident the discrepancy has something to do with the behavior of vtext numbers.

Was it helpful?

Solution

The email is being rejected because it doesn't look an email (there aren't any To From or Subject fields)

This works:

import smtplib

username = "account@gmail.com"
password = "password"

vtext = "1112223333@vtext.com"
message = "this is the message to be sent"

msg = """From: %s
To: %s
Subject: text-message
%s""" % (username, vtext, message)

server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(username,password)
server.sendmail(username, vtext, msg)
server.quit()

OTHER TIPS

The accepted answer didn't work for me with Python 3.3.3. I had to use MIMEText also:

import smtplib
from email.mime.text import MIMEText

username = "account@gmail.com"
password = "password"

vtext = "1112223333@vtext.com"
message = "this is the message to be sent"

msg = MIMEText("""From: %s
To: %s
Subject: text-message
%s""" % (username, vtext, message))

server = smtplib.SMTP('smtp.gmail.com',587)
# server.starttls()
server.login(username,password)
server.sendmail(username, vtext, msg.as_string())
server.quit()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top