0

I'm trying to send an email with an attachment from my local machine to a gmail account.

Here's my code:

import smtplib
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
from email import Encoders

def send_mail(send_from, send_to, body, text, files=None,server="127.0.0.1"):
    try:
        assert isinstance(send_to, list)
    except AssertionError as e:
        print e

    msg = MIMEMultipart(
        From=send_from,
        To=COMMASPACE.join(send_to),
        Date=formatdate(localtime=True),
        Subject=body
    )
    msg.attach(MIMEText(text))

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

    try:
        smtp = smtplib.SMTP(server)
        smtp.sendmail(send_from, send_to, msg.as_string())
        print "Successfully sent mail."
    except SMTPException:
        print "Error: unable to send email"

    smtp.close()

Based on the accepted answer of this question.

And here's the code that calls it:

send_from = "name.surname@my-company.com"
send_to = "lcuky_recipient@gmail.com"
subject = "testEmail", 
text = "This is a test. Hopefully, there's a file attached to it."
files = ".././dudewhat.csv"
send_mail(send_from, send_to, subject, text, files)

But this is what I'm getting in return:

File "belo_monthly.py", line 138, in <module>
    send_mail(send_from, send_to, subject, text, files)
  File "belo_monthly.py", line 26, in send_mail
    Subject=body
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/mime/multipart.py", line 36, in __init__
    MIMEBase.__init__(self, 'multipart', _subtype, **_params)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/mime/base.py", line 25, in __init__
    self.add_header('Content-Type', ctype, **_params)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/message.py", line 408, in add_header
    parts.append(_formatparam(k.replace('_', '-'), v))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/message.py", line 52, in _formatparam
    value = utils.encode_rfc2231(value[2], value[0], value[1])
IndexError: tuple index out of range

What did I miss?

Community
  • 1
  • 1
zack_falcon
  • 4,186
  • 20
  • 62
  • 108

2 Answers2

2

this is your problem:

subject = "testEmail",

remove comma at the end and things should work

DmitrySemenov
  • 9,204
  • 15
  • 76
  • 121
  • That did it, though now I'm getting an AssertionError. Would this warrant a new question? – zack_falcon Mar 27 '15 at 05:02
  • it's because your function assumes the list as parameter, while you send string variable. make send_to=[email1, email2, email3] or keep send_to=[email] in the case you send to singlar mail. I would refactor this function completely, doesn't look nice to me – DmitrySemenov Mar 27 '15 at 05:46
2

You are checking for instance for list but the parsed value is string. Try changing

send_to = "lcuky_recipient@gmail.com"

to

send_to = ["lcuky_recipient@gmail.com"]

or modify the logic

skar
  • 401
  • 5
  • 15