1

I have a text file to be sent via e-mail. I used the following code to send e-mail through smtplib. This code prints the attachment as e-mail body. Since my text file is bit larger all the content is not visible in mail body? How to display all the content in e-mail body? Any suggestions?

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
msg = MIMEMultipart()
msg['Subject'] = 'ANALYSIS REPORT'

filename = "report.txt"
f = file(filename)
attachment = MIMEText(f.read())
msg.attach(attachment)

smtpObj = smtplib.SMTP('mail.my-domain.com', 25)
smtpObj.sendmail(sender, receivers, msg.as_string())         
print "e-mail Successfully Sent!" 
Burhan Khalid
  • 152,028
  • 17
  • 215
  • 255
Nilani Algiriyage
  • 22,706
  • 29
  • 77
  • 114
  • 1
    What do you mean with 'not visible', not visible where? Is the file being truncated? And how big is 'big'? Have you considered compressing the text? – mata Jul 03 '13 at 10:36
  • @mata : In the body of the email last part of the text file is missing. My text file is consisted of tables printed using python pretty table. – Nilani Algiriyage Jul 03 '13 at 10:37
  • Maybe you're hitting a message size limit? You should check the return value of `smtpObj.sendmail(...)` – mata Jul 03 '13 at 10:39
  • @mata : Yes that should be the case! So what to do with the text file?can i send this as an attachment, then users can download it from the e-mail? – Nilani Algiriyage Jul 03 '13 at 10:45
  • Yes, send it as an attachment but even then it should be less than your envelope size limit from your server. – Burhan Khalid Jul 03 '13 at 10:54
  • @Burhan Khalid: How my code has to be modified? I couldn't find how to do it? – Nilani Algiriyage Jul 03 '13 at 10:56
  • [How to send Email Attachments with Python](http://stackoverflow.com/questions/3362600/how-to-send-email-attachments-with-python) – Burhan Khalid Jul 03 '13 at 11:01
  • @Burhan Khalid : I referred this too..Please see the comments at the bottom of the page, it still sends mail as in the body? – Nilani Algiriyage Jul 03 '13 at 11:07

1 Answers1

2

I would try to compress the content body, maybe that would get the message size down enough to get the mail through.

Example:

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.mime.application import MIMEApplication
from email.MIMEImage import MIMEImage
import io
import gzip

msg = MIMEMultipart()
msg['Subject'] = 'ANALYSIS REPORT'

msg.attach(MIMEText('report attached'))

filename = "report.txt"
with open(filename, 'rb') as f, io.BytesIO() as b:
    g = gzip.GzipFile(mode='wb', fileobj=b)
    g.writelines(f)
    g.close()
    attachment = MIMEApplication(b.getvalue(), 'x-gzip')
    attachment['Content-Disposition'] = 'attachment; filename=report.txt.gz'
msg.attach(attachment)

smtpObj = smtplib.SMTP('mail.my-domain.com', 25)
print smtpObj.sendmail(sender, receivers, msg.as_string())         
print "e-mail Successfully Sent!" 
mata
  • 60,113
  • 7
  • 139
  • 148