Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 截图并将其作为电子邮件附件发送_Python - Fatal编程技术网

Python 截图并将其作为电子邮件附件发送

Python 截图并将其作为电子邮件附件发送,python,Python,我正在尝试创建两个函数,一个是截图,然后调用另一个函数,该函数应该发送一封附有截图的电子邮件。如果它们分开,它们都可以工作,但当我试图将它们拼凑在一起时,会弹出一条巨大的错误消息 我试着把它放在同一块代码中,将它们分成两个函数,调用函数的不同方式。。。似乎什么都不管用 import smtplib from email import encoders from email.mime.base import MIMEBase from email.mime.multipart import MIM

我正在尝试创建两个函数,一个是截图,然后调用另一个函数,该函数应该发送一封附有截图的电子邮件。如果它们分开,它们都可以工作,但当我试图将它们拼凑在一起时,会弹出一条巨大的错误消息

我试着把它放在同一块代码中,将它们分成两个函数,调用函数的不同方式。。。似乎什么都不管用

import smtplib
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import os.path

def enviar_email():
    email = 'email'
    password = 'password'
    subject = 'This is the subject'
    message = 'This is my message'
    file_location = 'ss1.jpg'

    msg = MIMEMultipart()
    msg['From'] = email
    msg['To'] = email
    msg['Subject'] = subject

    msg.attach(MIMEText(message, 'plain'))

    # Setup the attachment
    filename = os.path.basename(file_location)
    attachment = open(file_location, "rb").read()
    image = MIMEImage(attachment, name=filename)
    msg.attach(image)


    # Attach the attachment to the MIMEMultipart object
    # msg.attach(part)


    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(email, password)
    text = msg.as_string()
    server.sendmail(email, email, text)
    server.quit()

def main():
    imagem = ImageGrab.grab()
    imagem.save('ss1.jpg', 'jpeg')
    enviar_email()

main()

***************************************
Error I get: 



RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
Traceback (most recent call last):
  File ".\enviar_email.py", line 51, in <module>
    main()
  File ".\enviar_email.py", line 47, in main
    imagem = ImageGrab.grab()
导入smtplib
从电子邮件导入编码器
从email.mime.base导入MIMEBase
从email.mime.multipart导入MIMEMultipart
从email.mime.text导入MIMEText
从email.mime.image导入MIMEImage
导入操作系统路径
def enviar_电子邮件():
电子邮件='email'
密码='password'
主题='这就是主题'
消息='这是我的消息'
文件位置='ss1.jpg'
msg=MIMEMultipart()
msg['From']=电子邮件
msg['To']=电子邮件
msg['Subject']=主语
msg.attach(MIMEText(消息'plain'))
#设置附件
filename=os.path.basename(文件位置)
附件=打开(文件位置,“rb”).read()
image=MIMEImage(附件,名称=文件名)
附加信息(图片)
#将附件附着到MIMEMultipart对象
#附加信息(部分)
server=smtplib.SMTP('SMTP.gmail.com',587)
server.starttls()
服务器登录(电子邮件、密码)
text=msg.as_string()
server.sendmail(电子邮件、电子邮件、文本)
server.quit()
def main():
imagem=ImageGrab.grab()
imagem.save('ss1.jpg','jpeg')
enviar_电子邮件()
main()
***************************************
我得到的错误是:
运行时错误:
已尝试在启动之前启动新进程
当前进程已完成其引导阶段。
这可能意味着您没有使用fork启动您的应用程序
子进程,而您忘记了使用正确的习惯用法
在主模块中:
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
冻结支持()
...
如果程序
不会冻结以生成可执行文件。
回溯(最近一次呼叫最后一次):
文件“\enviar_email.py”,第51行,在
main()
文件“\enviar_email.py”,第47行,主
imagem=ImageGrab.grab()

我这样做就解决了这个问题

if __name__ == '__main__':
    main()

我这样做就解决了这个问题

if __name__ == '__main__':
    main()