1

I have the below Python code for sending an e-mail to my ID from the contents of file filename. I am trying to send the email with the text color-formatted.

Any ideas please advise.

def ps_Mail():
    filename = "/tmp/ps_msg"
    f = file(filename)
    if os.path.exists(filename) and os.path.getsize(filename) > 0:
        mailp = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
        msg = MIMEMultipart('alternative')
        msg['To'] = "karn@abc.com"
        msg['Subject'] = "Uhh!! Unsafe rm process Seen"
        msg['From'] = "psCheck@abc.com"
        msg1 = MIMEText(f.read(),  'text')
        msg.attach(msg1)
        mailp.communicate(msg.as_string())
ps_Mail()
Daniel R. Livingston
  • 1,227
  • 14
  • 36
  • Why would you use `Popen()` to drive sendmail when there is the `smtplib` module? You'll have to wrap your text into HTML if that is not already done and then set the right `MIMEText` attribute – Anthon Jun 02 '17 at 07:15
  • 2
    You're going to want to use HTML to declare color. See [this related question](https://stackoverflow.com/questions/882712/sending-html-email-using-python) on how to do that. If, say, you wanted all of file `f` as one color, try: `msg1 = MIMEText("

    "+f.read()+"

    ", 'html')`
    – Daniel R. Livingston Jun 02 '17 at 07:19
  • 1
    The accepted answer to the question @DanielR.Livingston linked to also uses `smtplib`, as I suggested. – Anthon Jun 02 '17 at 07:24
  • @Anthon, Due to some cross platform checks and restriction i'm using Popen, though `smtplib` i understand better fit if fits into the environment. Moreover i'm dont want to use HTML as its Just a text message to pass on the e-mail. –  Jun 02 '17 at 07:57
  • @DanielR.Livingston, nice to have your suggestion. –  Jun 02 '17 at 07:59
  • @Anthon, this is not associated with the Duplicating with the tag you mentioned as i'm not looking for HTML way of , I'm Just looking if i can color the text contents simply. –  Jun 02 '17 at 08:04
  • @rockypy17 You wont get coloring more simple than using HTML – Anthon Jun 02 '17 at 08:58

1 Answers1

5

Here is the snippet I use to send HTML emails.

Please also read this

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

msg = MIMEMultipart('alternative')

msg['Subject'] = "Link"
msg['From'] = "my@email.com"
msg['To'] = "your@email.com"

text = "Hello World!"

html = """\
<html>
  <head></head>
  <body>
    <p style="color: red;">Hello World!</p>
  </body>
</html>
"""

part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1) # text must be the first one
msg.attach(part2) # html must be the last one

s = smtplib.SMTP('localhost')
s.sendmail(me, you, msg.as_string())
s.quit()
Szabolcs Dombi
  • 5,493
  • 3
  • 39
  • 71
  • Szabolcs , Your code looks good, even that i'm using already into my another code as a HTML format but here i dont want HTML nation of it. Its Just sending plain text. –  Jun 02 '17 at 08:00
  • 2
    AFAIK plaintext emails don't support colors. – Szabolcs Dombi Jun 02 '17 at 08:02