Reputation:
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"] = "[email protected]"
msg["To"] = "[email protected]"
msg["Subject"] = "python mail"
msg.attach(content)
p = Popen(["/usr/sbin/sendmail", "-t","-oi"], stdin=PIPE,universal_newlines=True)
p.communicate(msg.as_string())
Upvotes: 0
Views: 338
Reputation: 148870
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()
Upvotes: 1
Reputation: 19367
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")
Upvotes: 2