I have this piece of code:
l = ["Jargon", "Hello", "This", "Is", "Great"]
result = "\n".join(l[1:])
print result
output:
Hello
This
Is
Great
And I am trying to print this to a body of an email as shown below, I am getting the text as an attachment rather than as-body. can anyone please tell me if I am missing something here?
msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = emailto
ctype, encoding = mimetypes.guess_type(fileToSend)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
fp = open(file.csv, 'r')
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
fp.close()
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", fileame='file.csv')
msg.attach(attachment)
msg.attach(MIMEText(result, "plain"))
server = smtplib.SMTP("localhost")
server.sendmail(emailfrom, emailto, msg.as_string())
server.quit()
When using
yagmail
, it works as intended.where
conf_yag
stores your credentials,emailto
is the receiver email address, and'somefile.txt'
is the file attachment.