邮件发送Python-我的代码有什么问题?

时间:2019-01-30 15:26:32

标签: python html gmail

有人可以帮我吗?

我的代码有什么问题?

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

email = 'email@gmail.com'
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()

输出:

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()

我正在尝试合并两个不同的来源,但仍然失败。我想通过Gmail SMTP发送更多电子邮件,但是内容必须为HTML,并且电子邮件必须从.xlsx文件加载。有人吗?

谢谢,祝你有美好的一天!

0 个答案:

没有答案