在何处以及如何将附件放入电子邮件中,以便使用python发送

在何处以及如何将附件放入电子邮件中,以便使用python发送,python,email,email-attachments,Python,Email,Email Attachments,我最近学会了如何从文件夹中获取网络摄像头拍摄的最新照片。 现在我想知道我在哪里以及如何输入代码,所以我总是从文件夹中获取最后一张照片 这是我发送邮件的代码: #!/usr/bin/python import smtplib import userpass import attachment from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText i

我最近学会了如何从文件夹中获取网络摄像头拍摄的最新照片。 现在我想知道我在哪里以及如何输入代码,所以我总是从文件夹中获取最后一张照片

这是我发送邮件的代码:

#!/usr/bin/python

import smtplib
import userpass
import attachment
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os

def send_a_picture():
    gmail_user = userpass.fromaddr
    gmail_pwd = userpass.password

    def mail(to, subject, text, attach):
       msg = MIMEMultipart()

       msg['From'] = userpass.fromaddr
       msg['To'] = userpass.toaddr
       msg['Subject'] = subject

       msg.attach(MIMEText(text))

       part = MIMEBase('application', 'octet-stream')
       part.set_payload(open(attach, 'rb').read())
       Encoders.encode_base64(part)
       part.add_header('Content-Disposition',
               'attachment; filename="%s"' % os.path.basename(attach))
       msg.attach(part)

       mailServer = smtplib.SMTP("smtp.gmail.com", 587)
       mailServer.ehlo()
       mailServer.starttls()
       mailServer.ehlo()
       mailServer.login(gmail_user, gmail_pwd)
       mailServer.sendmail(gmail_user, to, msg.as_string())
       mailServer.close()

    mail(userpass.toaddr,
       "Hello from python!",
       "This is a email sent with python",
       "2017-01-17-19:47:44.jpg") # filename written into code


send_a_picture() 
当我指定一张照片的文件名时,这段代码(上面)工作得非常完美。 但是我想把这个代码放在下面的某个地方,这样我就可以得到网络摄像头拍摄的最新照片

def attach():

    import os
    import re

    files = os.listdir('/home/pi')
    jpgre = re.compile(r"\d{4}-\d{2}-\d{2}-\d{2}:\d{2}:\d{2}\.jpg")
    jpgs = [s for s in files if jpgre.match(s)]

    jpgs.sort()

    file = jpgs[-1]

    print (file)



# Output:       attachment.attach()
#               2017-01-17-19:47:44.jpg
在MKesper给了我一个建议(反复试验)之后,我试了一下。 我可以在代码中放置两个位置(attach()),而不会显示错误消息。但是没有邮件被送走

我想我需要更多的帮助。也许会有更多的帮助。 我实际上还是个初学者。只是想用艰苦的方式学习。 如果我是lycky,在成功之前只剩下几行代码

#!/usr/bin/python

import smtplib
import userpass
import attachment
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os

def send_a_picture():
    gmail_user = userpass.fromaddr
    gmail_pwd = userpass.password


**#tried the 'def attach()' in here.
# no error message but no mail.**


    def mail(to, subject, text, attach):
       msg = MIMEMultipart()

       msg['From'] = userpass.fromaddr
       msg['To'] = userpass.toaddr
       msg['Subject'] = subject

       msg.attach(MIMEText(text))

       part = MIMEBase('application', 'octet-stream')
       part.set_payload(open(attach, 'rb').read())
       Encoders.encode_base64(part)
       part.add_header('Content-Disposition',
               'attachment; filename="%s"' % os.path.basename(attach))
       msg.attach(part)

       mailServer = smtplib.SMTP("smtp.gmail.com", 587)
       mailServer.ehlo()
       mailServer.starttls()
       mailServer.ehlo()
       mailServer.login(gmail_user, gmail_pwd)
       mailServer.sendmail(gmail_user, to, msg.as_string())
       mailServer.close()

#**tried the 'def attach()' in here too.
# No error message but no mail here either.**

    mail("sjodin.ulf@gmail.com",
       "Hello from python!",
       "This is a email sent with python",
       "2017-01-16-23:39:46.jpg") # with the filename it works


send_a_picture() 
我找到了解决办法。

也许这不是最好的python代码。但这对我很有效。 同时也是一个起点,在这里我发现发展技能和编码的乐趣。 感谢提供Python电子邮件示例的提示。 我用了第三个,似乎是最接近我要找的。 这是我编写的代码:

import smtplib      #Import smtplib for the actual sending function
import userpass     #for my 'user', 'to', 'from' adresses and password
import glob         #for the variable 'last'
import os           #for the variable 'last'

# Here are the email package modules we'll need
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

COMMASPACE = ', '

last = last_photo_taken = sorted(glob.glob("/home/pi/*.jpg"),key=os.path.getmtime)[-1] 
#this is the variable I learned in my first question in this forum:
#Python code to get (latest) file with timestamp as name to attach to email



# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'Got the pyhon code working now. It has been a lot of Trial and Error.'
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] = userpass.fromaddr
msg['To'] = userpass.toaddr
msg.preamble = 'Image mail'

with open(last, 'rb') as fp:
    img = MIMEImage(fp.read())
    msg.attach(img)


mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(userpass.fromaddr, userpass.password)
mailServer.sendmail(userpass.fromaddr, userpass.toaddr, msg.as_string())
mailServer.close()

您的“attach”函数应该被调用为get_latest_jpg,并且只返回值,这样就可以在调用函数文件中使用它了。我想不需要嵌套函数。我想我需要一两个提示。之前尝试过一点(请参阅上面的新代码)。没用。好吧,那就试试教程/入门书吧。虽然还不错,但对你来说可能还是太陡了。也许会给你你需要的。哦,不要为仍在教授Python 2的内容而烦恼。