为什么我的手机上没有显示此文字?

时间:2014-09-11 02:48:23

标签: python function text smtp

以下是原始帖子的链接:Why am I not able to send a Colon in a message but am able to send a Semicolon??? (Python, SMTP module) a

这与当时的结肠有关。我尝试以不同的方式格式化,并且时间中的冒号使文本显示在我的手机上。这是我正在使用的短信功能的简单版本。它是Verizon iPhone的SMTP,如果这有所不同。我可以使用分号并且它可以工作,但冒号没有:

import time
current = (str(time.strftime("%I:%M %p")))

print (current)

def Text():
    import smtplib
    ContentMatch = ("Content match, website checked at: ", current)
    username = ("EmailUser")
    password = ("Password1")
    fromaddr = ("Username@gmail.com")
    toaddrs  = ("PhoneNumber@vtext.com")
    message = (str(ContentMatch))
    # The actual mail send
    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls()
    server.login(username,password)
    server.sendmail(fromaddr, toaddrs, message)
    server.quit()

Text()

1 个答案:

答案 0 :(得分:0)

您的讯息格式不正确。消息字符串必须是RFC2822兼容的消息。也就是说,它必须包含标题和可选主体。

试试这个:

message = "From: %s\n\n%s\n"%(fromaddr, ContentMatch)

但Python提供了用于格式化电子邮件的特殊类(例如email.mime.text.MIMEText),因此您不必手动执行此操作:

import time
import smtplib
from email.mime.text import MIMEText
from email.utils import formatdate

current = time.strftime("%I:%M %p")
message_text = "Content match, website checked at: %s" % current


def Text():
    # Parameters for sending
    username = "xxx"
    password = "xxx"
    fromaddr = "xxx@example.com"
    toaddr = "xxx@example.com"

    # Create the message. The only RFC2822-required headers
    # are 'Date' and 'From', but adding 'To' is polite
    message = MIMEText(message_text)
    message['Date'] = formatdate()
    message['From'] = fromaddr

    # Send the message
    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls()
    server.login(username, password)
    server.sendmail(fromaddr, toaddr, message.as_string())
    server.quit()

Text()