14

I am trying to use Python's smtplib to set the priority of an email to high. I have successfully used this library to send email, but am unsure how to get the priority working.

 import smtplib
 from smtplib import SMTP

My first attempt was to use this from researching how to set the priority:

smtp.sendmail(from_addr, to_addr, msg, priority ="high")

However I got an error: keyword priority is not recognized.

I have also tried using:

msg['X-MSMail-Priority'] = 'High'

However I get another error. Is there any way to set the priority using only smtplib?

4

1 回答 1

32

优先级只是电子邮件内容的问题(准确地说,是标题内容)。见这里

下一个问题是如何将其放入电子邮件中。

这完全取决于您如何构建该电子邮件。如果你使用该email模块,你会这样做:

from email.Message import Message
m = Message()
m['From'] = 'me'
m['To'] = 'you'
m['X-Priority'] = '2'
m['Subject'] = 'Urgent!'
m.set_payload('Nothing.')

然后将其与

smtp.sendmail(from_addr, to_addr, m.as_string())

值的附录:

根据这个论坛,有以下价值观:

1(最高)、2(高)、3(正常)、4(低)、5(最低)。如果省略该字段,则默认值为 3(正常)。

于 2012-08-07T10:38:53.190 回答