简体   繁体   English

在Python电子邮件中附加PNG图片

[英]Attaching a PNG image in Python Email

I'd like to send an email through python showing a logo. 我想通过python发送一封显示徽标的电子邮件。 The image is a png image in the same directory. 该图像是同一目录中的png图像。

I'm using a simple code I found on here for the purpose, but when I send it to my own account, there is no image. 我使用在这里找到的简单代码来实现此目的,但是当我将其发送到自己的帐户时,没有图像。 There is no attachment to be referenced to produce the image. 没有要引用的附件来生成图像。 Could someone please tell me what I am missing? 有人可以告诉我我在想什么吗?

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

Here is the code I am using to attach the image: 这是我用来附加图像的代码:

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

try this 尝试这个

add the header 添加标题

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

review this site 查看这个网站

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM