Search code examples
pythonsmtplib

SMTP sending an priority email


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?


Solution

  • 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.