0

I have the next code:

def send_email(self, alert_emails, subj, msg):
  global alert_from

  p = os.popen("/usr/sbin/sendmail -t" , 'w')
  p.write("To: %s\n" % (','.join(alert_emails),))
  p.write("From: %s\n" % (alert_from,))
  p.write("Subject: %s\n\n" % (subj,))
  p.write(msg)
  return p.close()  

It sends plain text messages. How can I change it to send HTML messages instead?

Thanks

KennyPowers
  • 4,925
  • 8
  • 36
  • 51
  • 1
    Already answered: http://stackoverflow.com/questions/882712/sending-html-email-in-python –  Feb 03 '12 at 15:22

2 Answers2

1

Use Content-Type: text/html header. And use smtplib to send emails.

Roman Bodnarchuk
  • 29,461
  • 12
  • 59
  • 75
0

Why would you use sendmail directly? Python has libraries for this sort of thing. Use the email module to create your mail, and smtplib to send it.

Plus there's no need to use a global statement unless you're modifying the global variable inside your function, which you're not.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • not sure why, but when I use smtplib all emails go to spam. I don't know why, but sendmail -t works ok with plaintext messages. I know it's lame :) may be you're right and I have to find why all emails go to spam using smtplib... – KennyPowers Feb 03 '12 at 15:23