I want to send email with attachments using a python script in Google Cloud Function Here is the code:
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
And got this error: FileNotFoundError: [Errno 2] No such file or directory: '/usr/sbin/sendmail': '/usr/sbin/sendmail'
Is this proc legal in gcp? or Is there any replacement for it? Thank you so much
With Cloud Functions you don't control your execution runtime, and you can't assume that a binary exist or not (and if it exists, it can disappear anytime). It's serverless! You only deploy your code
If you want more control on the environment runtime, you can use Cloud Run, very similar to Cloud Functions but you have to define your container and install all the dependencies that you want.
However, it won't work. The port 25 is blocked on Google cloud and you have to use external tools (sendgrid, twilio,...) to send emails.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.