发送电子邮件时主题未通过

时间:2017-05-04 18:04:38

标签: python email smtp mime

我发送的电子邮件如下:

def sendEmail(serial_number,date, time, latLon):

sender = serial_number+'@sdtr.com'
receivers = ['michael.grobman@station711.com']

msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = receivers
msg['Subject'] = 'Hello!'
msg = MIMEText("""        
    GPS fix

    Lat: %s
    Long: %s
    Time: %s
    Date: %s
    Altitude:
    Velocity: 

    Accuracy
    Horiz: +/- 16 m
    Vert: +/- 32 m

    Please note your reply is limited to 160 Latin characters or approximately 135 for non-Latin characters.

    Sent via bla. The mobile satellite company
    """ % (latLon[0], latLon[1], time, date))


smtpObj = smtplib.SMTP('bla', 587)
smtpObj.ehlo()
smtpObj.login('bla', 'bal')
smtpObj.sendmail(sender, receivers, msg.as_string())
smtpObj.quit()
print("Successfully sent email")

问题是在接收方,标题没有附加主题, 我已经尝试了几种不同的方式来指导我找到但仍然没有主题的相同结果。

1 个答案:

答案 0 :(得分:0)

我只使用MIMEText设法收到了有效的电子邮件。另外,我添加了2个非常有趣的标题(日期和消息ID):

from email.utils import make_msgid
from time import asctime


def sendEmail(serial_number, date, time, lat_lon):
    sender = serial_number + '@sdtr.com'
    receivers = ['michael.grobman@station711.com']

    message = """
GPS fix

Lat: %s
Long: %s
Time: %s
Date: %s
Altitude:
Velocity:

Accuracy
Horiz: +/- 16 m
Vert: +/- 32 m

Please note your reply is limited to 160 Latin characters or approximately 135 for non-Latin characters.

Sent via Inmarsat. The mobile satellite company
""" % (lat_lon[0], lat_lon[1], time, date)

    msg = MIMEText(message)
    msg['From'] = sender
    msg['To'] = ', '.join(receivers)
    msg['Subject'] = 'Hello!'
    msg['Message-ID'] = make_msgid()
    msg['Date'] = asctime()

    smtpObj = smtplib.SMTP('...', 587)
    smtpObj.login('...', '...')
    smtpObj.sendmail(sender, receivers, msg.as_string())
    smtpObj.quit()
    print('Successfully sent email')