2014-09-11 64 views
0

这里是原始帖子的链接:Why am I not able to send a Colon in a message but am able to send a Semicolon??? (Python, SMTP module) a为什么不在手机上显示此文本?

这与当时的冒号有关。我试着用不同的方式进行格式化,当时冒号使得手机上的文本显示为空白。这是我正在使用的短信功能的简单版本。这是一个威瑞森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 = ("[email protected]") 
    toaddrs = ("[email protected]") 
    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() 
+0

@paxdiablo没有我删除为原始。这是一个更简单的版本,因为其他人无法回答 – 2014-09-11 03:50:26

+1

道歉,普雷斯顿,当时我看,原来还在那里,它看起来像这只是一些额外的信息,可以更好地添加到该原件。我已经取消了这个,对于麻烦抱歉。您可能希望将一些原始信息添加到该信息中,因为您上面给出的链接仅在特定级别的代表中可见。 – paxdiablo 2014-09-11 03:55:55

回答

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 = "[email protected]" 
    toaddr = "[email protected]" 

    # 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() 
+0

谢谢!我会尝试一下,看看它是否有效! – 2014-09-11 14:07:31

相关问题