In Google cloud function, have FileNotFoundError: [Errno 2] No such file or directory: '/usr/sbin/sendmail': '/usr/sbin/sendmail'

In Google cloud function, have FileNotFoundError: [Errno 2] No such file or directory: '/usr/sbin/sendmail': '/usr/sbin/sendmail'

我想使用 Google 云函数中的 python 脚本发送带附件的电子邮件 这是代码:

def send_mail(text, from_user, to_user, subject):
    print ("SENDING EMAIL....")
    msg = MIMEMultipart()
    msg['From'] = from_user
    msg['To'] = ", ".join(to_user)
    msg['Subject'] = subject
    msg.attach(MIMEText(text, 'plain'))
    p = MIMEBase('application', 'octet-stream')
    attachment = open(join_path(OUTPUT_FILE_DIR, OUTPUT_FILENAME), "rb")
    p.add_header('Content-Disposition', "attachment; filename= %s"
                 % OUTPUT_FILENAME)
    p.set_payload(attachment.read())
    encoders.encode_base64(p)
    msg.attach(p)
    attachment.close()
    proc = Popen(['/usr/sbin/sendmail', '-t'], stdin=PIPE)
    proc.communicate(msg.as_string())
    return

并收到此错误:FileNotFoundError: [Errno 2] 没有这样的文件或目录:'/usr/sbin/sendmail': '/usr/sbin/sendmail'

这个proc在gcp中合法吗?或者有什么替代品吗?非常感谢

使用 Cloud Functions,您无法控制执行运行时,也不能假设二进制文件是否存在(如果存在,它随时可能消失)。它是无服务器的!您只需部署您的代码

如果您想对环境运行时有更多控制,可以使用 Cloud 运行,与 Cloud Functions 非常相似,但您必须定义容器并安装所需的所有依赖项。


但是,它不会起作用。端口 25 在 Google 云上被阻止,您必须使用外部工具(sendgrid、twilio 等)来发送电子邮件。