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


Messages In This Thread
Unable to send email attachments - by cosmarchy - Mar-06-2022, 08:02 PM
RE: Unable to send email attachments - by bowlofred - Mar-06-2022, 10:33 PM
RE: Unable to send email attachments - by cosmarchy - Mar-07-2022, 10:06 PM
RE: Unable to send email attachments - by bowlofred - Mar-07-2022, 10:16 PM
RE: Unable to send email attachments - by cosmarchy - Mar-08-2022, 09:04 PM
RE: Unable to send email attachments - by bowlofred - Mar-08-2022, 09:36 PM
RE: Unable to send email attachments - by cosmarchy - Mar-09-2022, 09:09 PM
RE: Unable to send email attachments - by bowlofred - Mar-09-2022, 09:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Send email with smtp without using Mimetext mgallotti 0 729 Feb-01-2023, 04:43 AM
Last Post: mgallotti
  email Library: properly send message as quoted-printable malonn 3 1,370 Nov-14-2022, 09:31 PM
Last Post: malonn
  code to send attachments contained on the drive. stefanoste78 1 914 Oct-12-2022, 02:16 AM
Last Post: Larz60+
  I get attachment paperclip on email without any attachments monika_v 5 2,059 Mar-19-2022, 10:20 PM
Last Post: cosmarchy
  How to make scraper send email notification just once themech25 0 1,408 Nov-08-2021, 01:51 PM
Last Post: themech25
  Sending Attachments via smtplib [SOLVED] AlphaInc 3 2,193 Oct-28-2021, 12:39 PM
Last Post: AlphaInc
  send email smtp rwahdan 0 1,812 Jun-19-2021, 01:28 AM
Last Post: rwahdan
  Need Outlook send email code using python srikanthpython 3 8,360 Feb-28-2021, 01:53 PM
Last Post: asyswow64
  How to send email using python? Gigux 2 2,896 Jul-04-2020, 07:53 AM
Last Post: Gigux
  I am trying to send an email with python nick235 9 5,404 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