通过Python和smtplib发送Verizon SMS消息

时间:2012-01-24 05:52:36

标签: python sms smtplib

我可以将smtplib发送到其他电子邮件地址,但出于某种原因,它无法发送到我的手机。

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()

当地址是gmail帐户时,邮件会成功发送,并且使用本机gmail界面向手机发送邮件非常有效。短信息号有何不同?

注意:使用set_debuglevel()我可以告诉smtplib认为消息是成功的,所以我相信这种差异与vtext数字的行为有关。

2 个答案:

答案 0 :(得分:4)

电子邮件被拒绝,因为它看起来不是电子邮件(没有任何To From或Subject字段)

这有效:

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()

答案 1 :(得分:1)

使用Python 3.3.3,我接受的答案对我不起作用。我还必须使用MIMEText:

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()
相关问题