主题行不在python发送的smtp邮件中

时间:2014-07-31 10:38:07

标签: python email smtp

我的代码正在运行并发送邮件。 但是主题没有收到邮件。这是我使用的代码片段。可能是什么问题?

代码:

   username = "N@EXAMPLE.com"
   password = "fnEOFINO”


   print("Logged in")

   sender = 'n@example.com'
   receivers = 'T@example.com'
   message = """From: Neeraja Rajiv  <NRajiv@EXAMPLE.com>
   To: T@EXAMPLE.COM 
   Subject: SMTP e-mail test

   Overall
   %d
                                               """%(variable)
   print("Connecting to server")

   server = smtplib.SMTP('SMTP-********.com', 25)
   print("Connected to server")
   server.set_debuglevel (1)
   server.sendmail(sender,receivers,message)         
   print ("Successfully sent email")

在电子邮件中,主题丢失。

欢迎任何方法/建议。

2 个答案:

答案 0 :(得分:0)

我认为您的问题在于如何创建消息,修正后的版本可以使用下面的示例代码:

msg = """From: 'From Neeraja Rajiv  <NRajiv@EXAMPLE.com>'
To: 'To Toffet Joseph - Consultant <TJoseph@EXAMPLE.com>'
Subject: 'SMTP e-mail test'
Overall'{}'""".format(variable)

username = "foo"
password = "bar"

print("Logged in")

import smtplib
sender = "foo@gmail.com"
receivers = "bar@gmail.com"
msg = """From: 'From Neeraja Rajiv  <NRajiv@EXAMPLE.com>'
To: 'To Toffet Joseph - Consultant <TJoseph@EXAMPLE.com>'
Subject: 'SMTP e-mail test'"""

server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(username, password)
server.sendmail(sender, receivers , msg)
server.quit()

答案 1 :(得分:0)

Padraic Cunningham的例子看起来很正确。这是另一个例子。也许这可能会有所帮助......

将电子邮件发送到SMTP服务器Python脚本示例:

导入smtplib

尝试:

host = '?.?.?.?'                                              #The Address To Your SMTP Server...
port = 25

to = 'reciever@someplace.com'                                 #Reciever Of Email...
from_addr = 'sender@someplace.com'                            #Sender Of Email...

subject = '<YOUR SUBJECT HERE>'                               #Subject Of Email...

text_line_1 = 'Body Line 1...\r\n'                            #Body Of Email...
text_line_2 = '\r\n'
text_line_3 = 'Body Line 3...\r\n'
text_line_4 = '\r\n'
text_line_5 = 'Body Line 5...\r\n'

total_message = text_line_1 + text_line_2 + text_line_3 + text_line_4 + text_line_5 

msg = "From: %s\nTo: %s\nSubject: %s\n\n%s" % (from_addr, [to], subject, total_message)

server = smtplib.SMTP(host, port)
server.sendmail(from_addr, to, msg)

print('Successfully Sent Email...')

除了例外e:     打印(&#39;例外:&#39; + str(e))

祝你好运......