通过Python和smtplib发送Verizon短信
我可以让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 个回答
2
这个被认可的回答在我用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()
4
这个邮件被拒绝了,因为它看起来不像一封邮件(没有收件人、发件人或主题字段)
这个是可以正常工作的:
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()