Python Forum
Mail sending Python - What it's wrong?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mail sending Python - What it's wrong?
#1
Hello guys, can someone help me?

What it's wrong in my code?

import pandas as pd
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

email = '[email protected]'
password = 'password'
e = pd.read_excel("Email.xlsx")
send_to_email = e['Emails'].values
subject = 'This is my blog'
messageHTML = '<p>Visit <a href="https://google.com">site<a> for some great <span style="color: #496dd0">tutorials and projects!</span><p>'
messagePlain = 'Visit for some great tutorials and projects!'

msg = MIMEMultipart('alternative')
msg['From'] = email
msg['To'] = send_to_email
msg['Subject'] = subject

msg.attach(MIMEText(messagePlain, 'plain'))
msg.attach(MIMEText(messageHTML, 'html'))

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()
Output:
Traceback (most recent call last):
  File "senderx.py", line 25, in <module>
    text = msg.as_string()
  File "/usr/lib/python2.7/email/message.py", line 137, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "/usr/lib/python2.7/email/generator.py", line 83, in flatten
    self._write(msg)
  File "/usr/lib/python2.7/email/generator.py", line 115, in _write
    self._write_headers(msg)
  File "/usr/lib/python2.7/email/generator.py", line 164, in _write_headers
    v, maxlinelen=self._maxheaderlen, header_name=h).encode()
  File "/usr/lib/python2.7/email/header.py", line 410, in encode
    value = self._encode_chunks(newchunks, maxlinelen)
  File "/usr/lib/python2.7/email/header.py", line 359, in _encode_chunks
    if not header:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Reply
#2
Check your line 16:
msg['To'] = send_to_email
I'm afraid that send_to_email is a list here and msg['To'] is expecting a string.
You can test it passing just a dummy email ([email protected]).
If this is the error and you still want to mail every email on this list (send_to_email) you'll have to iterate through it and send one at time.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,387 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Sending string commands from Python to a bluetooth device Rovelin 13 9,282 Aug-31-2021, 06:40 PM
Last Post: deanhystad
  Mail issue Mihil 3 2,589 Dec-03-2020, 05:25 AM
Last Post: Mihil
  Sending Out Email via Python JoeDainton123 1 4,699 Aug-31-2020, 12:54 AM
Last Post: nilamo
  Python sockets : Client sending to the server nico31780 0 2,276 May-17-2020, 07:56 PM
Last Post: nico31780
  problems sending BIN file with XMODEM python shaya2103 0 2,775 Nov-23-2019, 10:27 AM
Last Post: shaya2103
  python gives wrong string length and wrong character thienson30 2 2,943 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  smtplib mail without subject anna 2 2,431 Apr-24-2019, 05:44 AM
Last Post: anna
  Mail Faiyaz 1 23,783 Sep-24-2018, 02:29 PM
Last Post: Larz60+
  Python, sending E-mails Daxanius 1 2,799 Sep-05-2018, 03:49 PM
Last Post: buran

Forum Jump:

User Panel Messages

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