Python Forum

Full Version: Mail sending Python - What it's wrong?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
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.