【问题标题】:Attaching a PNG image in Python Email在 Python 电子邮件中附加 PNG 图像
【发布时间】:2017-03-22 04:42:53
【问题描述】:

我想通过 python 发送一封显示徽标的电子邮件。图片是同一目录下的png图片。

我正在使用我在这里找到的一个简单代码,但是当我将它发送到我自己的帐户时,没有图像。没有要引用的附件来生成图像。有人可以告诉我我缺少什么吗?

from tkinter import *
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import csv
import time
from string import Template
import warnings

root = Tk()
root.geometry('200x200')

email_label = Label(root, text="Enter your email")
email_label.pack()

username = Entry(root, width = 30)
username.pack()

password_label = Label(root, text="Enter your password")
password_label.pack()

password = Entry(root, show="*", width = 30)
password.pack()


def add_var():
    a = 0
    user_name = username.get()
    pass_word = password.get()
    with open("emailtk.csv") as f:
        reader = csv.reader(f)
        for row in reader:
            a+=1
            time.sleep(3)
            try:
                address = row[0]
                first_name = row[1]
                last_name = row[2]
                name = first_name+last_name
                company = row[4]
                print("Event", a)
                print("Will now send an email to %s %s at %s at %s" % (first_name, last_name, company, address))
                msg = MIMEMultipart('alternative')
                msg['Subject'] = "Link"
                msg['From'] = user_name
                msg['To'] = address
                html = """\
                <html>
                  <head></head>
                  <body>
                    <p>
                      stuff
                    </p>
                  </body>
                </html>
                """.format(n = name, org = company)
                part1 = MIMEText(html, 'html')
                msg.attach(part1)

                img = "logo.png"
                img_data = open(img, "rb").read()
                image = MIMEImage(img_data, name = os.path.basename(img))
                msg.attach(image)
                msg.attach(msgImage)

                s = smtplib.SMTP('Server.com', Socket)
                s.ehlo()
                s.starttls()
                s.login(user_name,pass_word)
                s.sendmail(user_name, address, msg.as_string())
                print("email sent")
                s.quit()
            except:
            pass
            print("Done")

button = Button(root, text = "Submit", command = add_var)
button.pack()

root.mainloop()

这是我用来附加图片的代码:

img = "logo.png"
img_data = open(img, "rb").read()
image = MIMEImage(img_data, name = os.path.basename(img))
msg.attach(image)

【问题讨论】:

  • 您应该在收到邮件时显示出现在邮件中的实际标题。如果您将其发送给其他平台上的某人,徽标会显示吗?如果您将其发送到同一平台上的不同帐户,徽标会显示吗?为了谨慎起见,请尝试在消息发送后 关闭文件 (s.sendmail(user_name, address, msg.as_string()); fp.close())
  • 我刚刚编辑了代码以显示我正在尝试的不同方法,但仍然没有运气。如果您指的是实际电子邮件中的标题,则那里没有附件。
  • 您应该显示您在问题中收到的实际标题。还要检查其他帐户或不同平台收到的邮件。

标签: python image email email-attachments


【解决方案1】:

试试这个

添加标题

msg.add_header('Content-Disposition', 'attachment', filename=filename)

查看此网站

https://docs.python.org/2/library/email-examples.html

【讨论】:

    猜你喜欢
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 2010-10-06
    • 2016-12-29
    • 2013-05-27
    • 2011-04-16
    • 1970-01-01
    相关资源
    最近更新 更多