Python Forum
Unable to send email attachments
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to send email attachments
#1
Hi,

I'm trying to end email attachments and have written the following:
import os
import smtplib
import random
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import encoders
from os.path import basename
from email.mime.application import MIMEApplication
from email.utils import COMMASPACE, formatdate

def send_mail(send_from, send_to, subject, text, files=None):

    msg = MIMEText(text)
    msg['From'] = send_from
    msg['To'] = COMMASPACE.join(send_to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject
    
    if isinstance(files, list):

        msg = MIMEMultipart()

        for f in files or []:
            with open(f, "rb") as fil:
                part = MIMEApplication(fil.read(),Name=basename(f))
            part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
            msg.attach(part)

    try:
        server = smtplib.SMTP_SSL('smtp.pobox.com', 465)
        server.ehlo()
        server.login("<email address>", "<password>")
        server.sendmail(msg['From'], msg['To'], msg.as_string())
        server.close()
        print ("successfully sent the mail")
    except:
        print ("failed to send mail")

send_mail("<email address>",[<email address>],"subject","text",["D:\\Desktop\\Python Test\\Logfile.txt"]) # with attachment
#send_mail("<email address>",[<email address>],"subject","text") # without attachment
the idea being that I can use the same function whether I want to send an email with or without an attachment - all I have to do is leave the attachment argument empty...

When I run this with the attachment (second to last line) I get the following:
Output:
PS D:\Desktop\Python Test> & C:/Users/me/AppData/Local/Programs/Python/Python39/python.exe "d:/Desktop/Python Test/SendEmail2.py" failed to send mail
but when I send email without attachment (last line) I get the following:
Output:
PS D:\Desktop\Python Test> & C:/Users/me/AppData/Local/Programs/Python/Python39/python.exe "d:/Desktop/Python Test/SendEmail2.py" successfully sent the mail
So, I can rule out login issues as appears to work when not sending attachments.

If someone could assist with this why the attachment part of the code produces an error please, I would appreciate it.

Thanks
Reply
#2
Your try/except is consuming the exception and throwing away all the information it contained. That's a poor way to handle errors.

If you're not going to do something with the exception, better to just let the exception end the program and display the message contents. You could also print or return the exception contents, but that's more work. It would let you see what line is erroring and what the error is.
ibreeden likes this post
Reply
#3
If I print the error I get this:
Output:
'NoneType' object has no attribute 'strip'
Stepping through the code, the error occurs here:
Output:
server.sendmail(msg['From'], msg['To'], msg.as_string())
I believe the error is trying to indicate there is no strip function for an object but I'm not sure how this relates to the line on which it occurs...
Reply
#4
Please show the entire traceback. It has lots of valuable information. It's not clear to me if the msg method is bombing or if the sendmail doesn't like the message.
Reply
#5
Hi,

I modified this code to print the error:
try:
        server = smtplib.SMTP_SSL('smtp.pobox.com', 465)
        server.ehlo()
        server.login("", "")
        server.sendmail(msg['From'], msg['To'], msg.as_string()) #error here
        server.close()
        print ("successfully sent the mail")
    except Exception as e:
        print ("failed to send mail")
        print (e)
All I get in the terminal is this:
Output:
PS D:\Desktop\Python Test> & C:/Users/me/AppData/Local/Programs/Python/Python39/python.exe "d:/Desktop/Python Test/SendEmail2.py" failed to send mail 'NoneType' object has no attribute 'strip' PS D:\Desktop\Python Test>
Reply
#6
You're still consuming the traceback so I can't tell what function is erroring. Maybe try printing out the msg.as_string() before sending. It may either fail (which tells you more about where to look), or you might see something wrong in the output.
Reply
#7
This is what I get for msg.as_string()

Output:
PS D:\Desktop\Python Test> & C:/Users/me/AppData/Local/Programs/Python/Python39/python.exe "d:/Desktop/Python Test/SendEmail2.py" failed to send mail 'NoneType' object has no attribute 'strip' Content-Type: multipart/mixed; boundary="===============5857163676093379878==" MIME-Version: 1.0 --===============5857163676093379878== Content-Type: application/octet-stream; Name="Logfile.txt" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="Logfile.txt" DQo3NrBDIDIxIERlYyAyMDIxIDIyOjA4OjM3IFRlbXBlcmF0dXJlIENyaXRpY2FsLCBpbml0aWF0 aW5nIHNodXRkb3duDQoNCjcysEMgMjEgRGVjIDIwMjEgMjI6MDg6MzkgVGVtcGVyYXR1cmUgQ3Jp dGljYWwsIGluaXRpYXRpbmcgc2h1dGRvd24NCg0KNzawQyAyMSBEZWMgMjAyMSAyMjowODo0MSBU ZW1wZXJhdHVyZSBDcml0aWNhbCwgaW5pdGlhdGluZyBzaHV0ZG93bg0KDQo0MbBDIDIxIERlYyAy MDIxIDIyOjA4OjQzDQoNCjQ1sEMgMjEgRGVjIDIwMjEgMjI6MDg6NDUNCg0KNzWwQyAyMSBEZWMg MjAyMSAyMjowODo0NyBUZW1wZXJhdHVyZSBDcml0aWNhbCwgaW5pdGlhdGluZyBzaHV0ZG93bg0K DQo2NLBDIDIxIERlYyAyMDIxIDIyOjA4OjQ5IFRlbXBlcmF0dXJlIENyaXRpY2FsLCBpbml0aWF0 aW5nIHNodXRkb3duDQoNCjc2sEMgMjEgRGVjIDIwMjEgMjI6MDg6NTEgVGVtcGVyYXR1cmUgQ3Jp dGljYWwsIGluaXRpYXRpbmcgc2h1dGRvd24NCg0KNDWwQyAyMSBEZWMgMjAyMSAyMjowODo1Mw0K DQo1NLBDIDIxIERlYyAyMDIxIDIyOjA4OjU1DQoNCjczsEMgMjEgRGVjIDIwMjEgMjI6MDg6NTcg VGVtcGVyYXR1cmUgQ3JpdGljYWwsIGluaXRpYXRpbmcgc2h1dGRvd24NCg0KNTewQyAyMSBEZWMg MjAyMSAyMjowODo1OQ0KDQo3NLBDIDIxIERlYyAyMDIxIDIyOjA5OjAxIFRlbXBlcmF0dXJlIENy aXRpY2FsLCBpbml0aWF0aW5nIHNodXRkb3duDQoNCjc2sEMgMjEgRGVjIDIwMjEgMjI6MDk6MDMg VGVtcGVyYXR1cmUgQ3JpdGljYWwsIGluaXRpYXRpbmcgc2h1dGRvd24NCg0KNTKwQyAyMSBEZWMg MjAyMSAyMjowOTowNQ0K --===============5857163676093379878==--
not sure whether this helps or not???
Reply
#8
You're running a program there, so I can't tell which bits of the output are from the command requested and which bits are from some other part of the program. Did the 'NoneType' object has no attribute 'strip' come from that command as well?

If you have any try/except in that script, remove them. A simple traceback should have pointed at the location that is causing problems.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Send email with smtp without using Mimetext mgallotti 0 715 Feb-01-2023, 04:43 AM
Last Post: mgallotti
  email Library: properly send message as quoted-printable malonn 3 1,342 Nov-14-2022, 09:31 PM
Last Post: malonn
  code to send attachments contained on the drive. stefanoste78 1 869 Oct-12-2022, 02:16 AM
Last Post: Larz60+
  I get attachment paperclip on email without any attachments monika_v 5 2,003 Mar-19-2022, 10:20 PM
Last Post: cosmarchy
  How to make scraper send email notification just once themech25 0 1,392 Nov-08-2021, 01:51 PM
Last Post: themech25
  Sending Attachments via smtplib [SOLVED] AlphaInc 3 2,171 Oct-28-2021, 12:39 PM
Last Post: AlphaInc
  send email smtp rwahdan 0 1,802 Jun-19-2021, 01:28 AM
Last Post: rwahdan
  Need Outlook send email code using python srikanthpython 3 8,276 Feb-28-2021, 01:53 PM
Last Post: asyswow64
  How to send email using python? Gigux 2 2,865 Jul-04-2020, 07:53 AM
Last Post: Gigux
  I am trying to send an email with python nick235 9 5,327 Jun-29-2020, 06:40 PM
Last Post: nick235

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020