0

I am getting an additional content as given bellow when I am sending mail from unix server using python sendmail.This content is displayed in the mail.

From nobody Mon Dec 18 09:36:01 2017 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit

My code is as follows.

   #reading data from file
   data = MIMEText(file('%s'%file_name).read())
   #writing the content as html
   content = MIMEText("<!DOCTYPE html><html><head><title></title></head><body>"+'%s'%data+"</body></html>", "html")
   msg = MIMEMultipart("alternative")

   msg["From"] = "sender@mail.com"
   msg["To"] = "mymailid@mail.com"
   msg["Subject"] = "python mail"

   msg.attach(content)

   p = Popen(["/usr/sbin/sendmail", "-t","-oi"], stdin=PIPE,universal_newlines=True)
   p.communicate(msg.as_string())
  • I just want to avoid that, warning. My file content is an html data, but when I receive mail, I am seeing this warning above my message. I want to disable that. –  Dec 18 '17 at 09:54
  • I suspect you also need to explicitly identify `data` as being "html". `data = MIMEText(whatever, "html")`. – Andy G Dec 18 '17 at 09:55
  • Possible duplicate of https://stackoverflow.com/questions/7755501/embed-picture-in-email/7755994#7755994 – tripleee Dec 18 '17 at 11:05

2 Answers2

2

You are constructing the email content in two parts, as data and content. You need to explicitly confirm that both are HTML. So change

data = MIMEText(file('%s'%file_name).read())

to

data = MIMEText(file('%s'%file_name).read(), "html")
Andy G
  • 19,232
  • 5
  • 47
  • 69
  • Thanks, Actually my mistake was, I have added to multiple HTML headers to the data, the step: `content = MIMEText(" "+'%s'%data+"", "html")` was not necessary, just reading the file content as HTML is fine. –  Dec 18 '17 at 16:38
1

You should look at the message string. The message you see is not a warning, it is just what you have writen into the message with:

data = MIMEText(file('%s'%file_name).read())
content = MIMEText("<!DOCTYPE html><html><head><title></title></head><body>"
    +'%s'%data+"</body></html>", "html")

data.as_string() actually contains Content-Type: text/plain; ... because it has been added by the first MIMEText line, when you want to include it into the body of a HTML page.

What you really want is probably:

data = file(file_name).read()
content = MIMEText("<!DOCTYPE html><html><head><title></title></head><body>"
    +'%s'%data+"</body></html>", "html")

But I also think that you do not need to include it into another level with a MIMEMultipart("alternative"): msg = content is probably enough.

Finally, I do not think that explicitely starting a new process to execute sendmail is really overkill, when the smtplib module from the standard library aloready knows how to send a message:

import smtplib

server = smtplib.SMTP()
server.send_message(msg)
server.quit()
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Thanks, Actually my mistake was, I have added to multiple HTML headers to the data, the step: `content = MIMEText(" "+'%s'%data+"", "html")` was not necessary, just reading the file content as HTML is fine. –  Dec 18 '17 at 16:39