【问题标题】:Sending Verizon SMS message via Python and smtplib通过 Python 和 smtplib 发送 Verizon SMS 消息
【发布时间】:2012-01-24 05:52:36
【问题描述】:

我可以让 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 数字的行为有关。

【问题讨论】:

    标签: python sms smtplib


    【解决方案1】:

    电子邮件被拒绝,因为它看起来不像电子邮件(没有任何收件人或主题字段)

    这行得通:

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

    【讨论】:

    • 谢谢!我之前曾尝试添加 To 和 From 字段,但我忘记了主题字段。完美运行!
    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 2017-04-02
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 2017-09-03
      • 2019-01-20
      相关资源
      最近更新 更多