<legend id='TlxtJ'><style id='TlxtJ'><dir id='TlxtJ'><q id='TlxtJ'></q></dir></style></legend>
  • <small id='TlxtJ'></small><noframes id='TlxtJ'>

        • <bdo id='TlxtJ'></bdo><ul id='TlxtJ'></ul>
        <tfoot id='TlxtJ'></tfoot>
        <i id='TlxtJ'><tr id='TlxtJ'><dt id='TlxtJ'><q id='TlxtJ'><span id='TlxtJ'><b id='TlxtJ'><form id='TlxtJ'><ins id='TlxtJ'></ins><ul id='TlxtJ'></ul><sub id='TlxtJ'></sub></form><legend id='TlxtJ'></legend><bdo id='TlxtJ'><pre id='TlxtJ'><center id='TlxtJ'></center></pre></bdo></b><th id='TlxtJ'></th></span></q></dt></tr></i><div id='TlxtJ'><tfoot id='TlxtJ'></tfoot><dl id='TlxtJ'><fieldset id='TlxtJ'></fieldset></dl></div>

        Gmail Python 多个附件

        Gmail Python multiple attachments(Gmail Python 多个附件)
            <i id='fvvUF'><tr id='fvvUF'><dt id='fvvUF'><q id='fvvUF'><span id='fvvUF'><b id='fvvUF'><form id='fvvUF'><ins id='fvvUF'></ins><ul id='fvvUF'></ul><sub id='fvvUF'></sub></form><legend id='fvvUF'></legend><bdo id='fvvUF'><pre id='fvvUF'><center id='fvvUF'></center></pre></bdo></b><th id='fvvUF'></th></span></q></dt></tr></i><div id='fvvUF'><tfoot id='fvvUF'></tfoot><dl id='fvvUF'><fieldset id='fvvUF'></fieldset></dl></div>
            • <bdo id='fvvUF'></bdo><ul id='fvvUF'></ul>

              <small id='fvvUF'></small><noframes id='fvvUF'>

            • <tfoot id='fvvUF'></tfoot>
                  <tbody id='fvvUF'></tbody>
                • <legend id='fvvUF'><style id='fvvUF'><dir id='fvvUF'><q id='fvvUF'></q></dir></style></legend>

                  本文介绍了Gmail Python 多个附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试创建一个小脚本,该脚本将使用 gmail 发送多个附件.下面的代码发送电子邮件但不发送附件.预期用途是 cron 几个数据库查询并通过电子邮件发送结果.总会有 2 个文件,而且文件名每天都不同,因为报告的 date 在文件名中.否则我会使用:

                  I am trying to create a little script that will email multiple attachments using gmail. The code below sends the email but not the attachments. The intended use is to cron a couple db queries and email the results. There will always be 2 files and the file names will be different each day as the date for the report is in the file name. Otherwise I would have just used:

                  part.add_header('Content-Disposition', 
                      'attachment; filename="absolute Path for the file/s"')
                  

                  非常感谢任何帮助.

                  import os
                  import smtplib
                  from email.MIMEMultipart import MIMEMultipart
                  from email.MIMEText import MIMEText
                  from email.MIMEImage import MIMEImage
                  from email.MIMEBase import MIMEBase
                  from email import Encoders
                  
                  
                  #Set up crap for the attachments
                  files = "/tmp/test/dbfiles"
                  filenames = [os.path.join(files, f) for f in os.listdir(files)]
                  #print filenames
                  
                  
                  #Set up users for email
                  gmail_user = "joe@email.com"
                  gmail_pwd = "somepasswd"
                  recipients = ['recipient1','recipient2']
                  
                  #Create Module
                  def mail(to, subject, text, attach):
                     msg = MIMEMultipart()
                     msg['From'] = gmail_user
                     msg['To'] = ", ".join(recipients)
                     msg['Subject'] = subject
                  
                     msg.attach(MIMEText(text))
                  
                     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())
                     # Should be mailServer.quit(), but that crashes...
                     mailServer.close()
                  
                  #get all the attachments
                     for file in filenames:
                        part = MIMEBase('application', 'octet-stream')
                        part.set_payload(open(file, 'rb').read())
                        Encoders.encode_base64(part)
                        part.add_header('Content-Disposition', 'attachment; filename="%s"'
                                     % os.path.basename(file))
                        msg.attach(part)
                  #send it
                  mail(recipients,
                     "Todays report",
                     "Test email",
                     filenames)
                  

                  推荐答案

                  应该再等一个小时才能发帖.进行了 2 处更改:

                  Should have waited another hour before posting. Made 2 changes:

                  1.) 将附件循环向上移动

                  1.) moved the attachment loop up

                  2.) 换出part.add_header('Content-Disposition', '附件; filename="%s"'% os.path.basename(文件))

                  2.) swapped out part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))

                  for part.add_header('Content-Disposition', 'attachment; filename="%s"' % file)

                  for part.add_header('Content-Disposition', 'attachment; filename="%s"' % file)

                  像冠军一样工作.具有多个收件人和多个附件的 Gmail.

                  Works like a champ. Gmail with multiple recipients and multiple attachments.

                  import os 
                  import smtplib
                  from email.MIMEMultipart import MIMEMultipart
                  from email.MIMEText import MIMEText
                  from email.MIMEImage import MIMEImage
                  from email.MIMEBase import MIMEBase
                  from email import Encoders
                  
                  
                  #Set up crap for the attachments
                  files = "/tmp/test/dbfiles"
                  filenames = [os.path.join(files, f) for f in os.listdir(files)]
                  #print filenames
                  
                  
                  #Set up users for email
                  gmail_user = "joe@email.com"
                  gmail_pwd = "somepasswd"
                  recipients = ['recipient1','recipient2']
                  
                  #Create Module
                  def mail(to, subject, text, attach):
                     msg = MIMEMultipart()
                     msg['From'] = gmail_user
                     msg['To'] = ", ".join(recipients)
                     msg['Subject'] = subject
                  
                     msg.attach(MIMEText(text))
                  
                     #get all the attachments
                     for file in filenames:
                        part = MIMEBase('application', 'octet-stream')
                        part.set_payload(open(file, 'rb').read())
                        Encoders.encode_base64(part)
                        part.add_header('Content-Disposition', 'attachment; filename="%s"' % file)
                        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())
                     # Should be mailServer.quit(), but that crashes...
                     mailServer.close()
                  
                  #send it
                  mail(recipients,
                     "Todays report",
                     "Test email",
                     filenames)
                  

                  这篇关于Gmail Python 多个附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                    <tfoot id='AlIs5'></tfoot>
                    <i id='AlIs5'><tr id='AlIs5'><dt id='AlIs5'><q id='AlIs5'><span id='AlIs5'><b id='AlIs5'><form id='AlIs5'><ins id='AlIs5'></ins><ul id='AlIs5'></ul><sub id='AlIs5'></sub></form><legend id='AlIs5'></legend><bdo id='AlIs5'><pre id='AlIs5'><center id='AlIs5'></center></pre></bdo></b><th id='AlIs5'></th></span></q></dt></tr></i><div id='AlIs5'><tfoot id='AlIs5'></tfoot><dl id='AlIs5'><fieldset id='AlIs5'></fieldset></dl></div>
                    • <bdo id='AlIs5'></bdo><ul id='AlIs5'></ul>
                            <tbody id='AlIs5'></tbody>
                        • <small id='AlIs5'></small><noframes id='AlIs5'>

                            <legend id='AlIs5'><style id='AlIs5'><dir id='AlIs5'><q id='AlIs5'></q></dir></style></legend>