![]() |
Unable to send email attachments - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Unable to send email attachments (/thread-36572.html) |
Unable to send email attachments - cosmarchy - Mar-06-2022 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 attachmentthe 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: but when I send email without attachment (last line) I get the following: 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 RE: Unable to send email attachments - bowlofred - Mar-06-2022 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. RE: Unable to send email attachments - cosmarchy - Mar-07-2022 If I print the error I get this: Stepping through the code, the error occurs here: 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...
RE: Unable to send email attachments - bowlofred - Mar-07-2022 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. RE: Unable to send email attachments - cosmarchy - Mar-08-2022 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:
RE: Unable to send email attachments - bowlofred - Mar-08-2022 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. RE: Unable to send email attachments - cosmarchy - Mar-09-2022 This is what I get for msg.as_string() not sure whether this helps or not???
RE: Unable to send email attachments - bowlofred - Mar-09-2022 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. |