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?
Compose the mail message. Click on “Options,” > “Priority,” and then locate the level of importance to mark this message and click once. Click “Send.”
Most email services and programs use message priorities to designate what's important and what's not. Message priorities can be applied manually or automatically, and you can also mark a message as low priority, or unimportant.
The 'high priority' sending option is designed to ensure that the recipient of the email knows that this is an important email before opening it and should therefore be prioritised. It may be that the email Page 2 contains urgent information that the recipient may need.
String priority = msg. getHeader("X-Priority", ""); if (priority != null && priority. equals("1")) ... // it's important Similarly for the more standard, but less used, Importance and Priority headers.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With