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?

3
  • 4
    And what is "another error" exactly? Apart from that: you're invening new API calls. The sendmail() method does not take a 'priority' parameter - why don't you check the API docs? sendmail() accepts a mail_options parameter which is a list of parameters..don't guess API, look them up. Aug 7, 2012 at 9:47
  • I can not find any documentation on the web specifying the mail_options parameters! I've unsuccessfully attempted this: smtp.sendmail(from_addr, to_addr, msg[,mail_options=[priority="high"]])
    – Sarah92
    Aug 7, 2012 at 10:18
  • @SarahAddis The docs clearly state that "The caller may pass a list of ESMTP options (such as 8bitmime) to be used in MAIL FROM commands as mail_options. ESMTP options (such as DSN commands) that should be used with all RCPT commands can be passed as rcpt_options." You'll have to look up in the RFCs what ESMPT options and RCPT options are.
    – glglgl
    Aug 7, 2012 at 10:46

1 Answer 1

36

Priority is just a matter of email content (to be exact, header content). See here.

The next question would be how to put that into an email.

That completely depends how you build that email. If you use the email module, you would do it this way:

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

and then use it with

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

Addendum for the values:

According to this forum, there are the following values:

1 (Highest), 2 (High), 3 (Normal), 4 (Low), 5 (Lowest). 3 (Normal) is default if the field is omitted.

5
  • How to on "Flag" when sending a mail? I tried the above with an additional line as "m['Flag'] = '1'". I didn't get any error but the flag wasn't on either.
    – zingy
    Oct 15, 2015 at 10:25
  • 1
    @zingy use priority 2, it will give your flag
    – Busturdust
    Oct 21, 2015 at 19:56
  • @Busturdust With priority '2' only exclamation sign appeared but the flag was not enabled.
    – zingy
    Oct 26, 2015 at 9:24
  • What if i want to remove the priority flag? I tried with the empty string for X-Priority header but no change. Apr 24, 2018 at 10:33
  • just one question, why number 2 ?, I thought that "1" is the highest priority. Aug 20, 2020 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.