1

I wrote a program that sends an email with results using the python smtp library:

def send_mail(sender_mail, receiver_mail):
    msg = MIMEMultipart()
    msg['From'] = sender_mail
    msg['To'] = receiver_mail
    msg['Subject'] = "Test results"
    file = open(RESULTS_FILE, "r")
#   body = file.read()
    ##attachment = MIMEText(file.read())
    attachment = MIMEBase('application', "octet-stream")
    attachment.set_payload(file.read())
    file.close()
    attachment.add_header('Content-Disposition', 'attachment', 
    filename=RESULTS_FILE)
    msg.attach(attachment)
    body = "Attached the test results"
    msg.attach(MIMEText(body, 'plain'))

    server = smtplib.SMTP("172.16.100.9", 25)

    #server.starttls()
    #server.login(sender_mail, password)
    text = msg.as_string()
    server.sendmail(sender_mail, receiver_mail, text)
    server.quit()

But when i try to run it, it seems that the line:text = msg.as_string() leads to the error:

    Traceback (most recent call last):
  File "\\filer2\incam\test_scripts\run_only_ci.py", line 268, in <module>
    testresults.analyze_tests(TESTS_FOLDER, RESULTS_FOLDER, sender_mail, receiver_mail)
  File "\\filer2\incam\test_scripts\testresults.py", line 224, in analyze_tests
    send_mail(sender_mail, receiver_mail)
  File "\\filer2\incam\test_scripts\testresults.py", line 119, in send_mail
    text = msg.as_string()
  File "C:\Users\fl_ncc\AppData\Local\Programs\Python\Python36\lib\email\message.py", line 158, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "C:\Users\fl_ncc\AppData\Local\Programs\Python\Python36\lib\email\generator.py", line 116, in flatten
    self._write(msg)
  File "C:\Users\fl_ncc\AppData\Local\Programs\Python\Python36\lib\email\generator.py", line 195, in _write
    self._write_headers(msg)
  File "C:\Users\fl_ncc\AppData\Local\Programs\Python\Python36\lib\email\generator.py", line 222, in _write_headers
    self.write(self.policy.fold(h, v))
  File "C:\Users\fl_ncc\AppData\Local\Programs\Python\Python36\lib\email\_policybase.py", line 326, in fold
    return self._fold(name, value, sanitize=True)
  File "C:\Users\fl_ncc\AppData\Local\Programs\Python\Python36\lib\email\_policybase.py", line 369, in _fold
    parts.append(h.encode(linesep=self.linesep, maxlinelen=maxlinelen))
AttributeError: 'tuple' object has no attribute 'encode'

Why does it happen? It worked before so i dont understand why it happens. Thanks for you help.

Adamso
  • 97
  • 1
  • 10
  • What are the value of `sender_mail` and `receiver_mail`? – Peter Wood Sep 02 '18 at 13:15
  • if it worked before - what did you change? – Cut7er Sep 02 '18 at 13:17
  • 3
    Please check you do not have a comma `,` at the end of the line somewhere in your code. E.g `a = 'hello',`. This makes a tuple – awesoon Sep 02 '18 at 13:17
  • See this question: [How to send email to multiple recipients using python smtplib?](https://stackoverflow.com/questions/8856117/how-to-send-email-to-multiple-recipients-using-python-smtplib) – Peter Wood Sep 02 '18 at 13:18

1 Answers1

0

Attention your code "msg['To'] = receiver_mail" in function of send_mail,which is not necessary becuase sendmail has parameter for receiver_mail like the flowing.In other word, you don't have to emphasize receiver_mail in msg['To'], msg['To'] merely shows information of receivers in mail.

server.sendmail(sender_mail, receiver_mail, text)

The error happens when msg['To'] is assigned with list like ['XXX@XXX','XXX@XXX'].If you set it with single string like "msg['To']='XXX@XXX'", it will be ok.Then you can use "for loop" statements when number of receiver_mail's elements >1. You can also comment related code like the flowing, to avoid "for loop" statements:

# msg['To'] = receiver_mail
Janeasefor
  • 23
  • 3
  • If you want to display all information of receiver ,please try the flowing code: `msg['To'] = ','.join(receiver_mail)` – Janeasefor Aug 26 '21 at 04:35