Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.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 运行sendgrid脚本可以手动工作,但在crontab下运行时失败_Python_Ubuntu_Cron_Sendgrid - Fatal编程技术网

Python 运行sendgrid脚本可以手动工作,但在crontab下运行时失败

Python 运行sendgrid脚本可以手动工作,但在crontab下运行时失败,python,ubuntu,cron,sendgrid,Python,Ubuntu,Cron,Sendgrid,我的目标是每周使用sendgridpython包向多个涉众发送一个通过R脚本创建的pdf文件。手动运行R脚本和python邮件脚本时,邮件和附件会成功发送。但当我在cron作业下运行它们时,邮件无法发送 创建的pdf文件是test.pdf R脚本的位置是 /home/username/script.R /home/username/run.py python脚本的位置是 /home/username/script.R /home/username/run.py run.py的内容如下 i

我的目标是每周使用
sendgrid
python包向多个涉众发送一个通过R脚本创建的pdf文件。手动运行R脚本和python邮件脚本时,邮件和附件会成功发送。但当我在cron作业下运行它们时,邮件无法发送

创建的pdf文件是
test.pdf

R脚本的位置是

/home/username/script.R
/home/username/run.py
python脚本的位置是

/home/username/script.R
/home/username/run.py
run.py
的内容如下

import base64
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import (
  Mail, Attachment, FileContent, FileName,
  FileType, Disposition, ContentId)
from sendgrid.helpers.mail import Mail

message = Mail(
  from_email='from_email@gmail.com',
  to_emails=['to_email1@gmail.com','to_email_2@gmail.com','to_email_3@gmail.com','to_email_4@gmail.com'],
  subject='Mail',
  html_content='<strong>Please find attached</strong>')
file_path = 'test.pdf'
with open(file_path, 'rb') as f:
  data = f.read()
f.close()
encoded = base64.b64encode(data).decode()
attachment = Attachment()
attachment.file_content = FileContent(encoded)
attachment.file_type = FileType('application/pdf')
attachment.file_name = FileName('test_filename.pdf')
attachment.disposition = Disposition('attachment')
attachment.content_id = ContentId('Example Content ID')
message.attachment = attachment
try:
  sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
  response = sendgrid_client.send(message)
  print(response.status_code)
  print(response.body)
  print(response.headers)
except Exception as e:
  print(e.message)
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:        18.04
Codename:       bionic

使用
grepcron/var/log/syslog
查看cron日志时,我看到以下消息

CRON[27260]: (username) MAIL (mailed 1 byte of output but got status 0x0001 from MTA#012)

Python脚本在手动条件下运行良好,但在cron上自动运行时失败。我已经按照
sendgrid
python包完成了必要的API验证。关于
crontab
,是否有我遗漏的重要设置? 操作系统如下

import base64
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import (
  Mail, Attachment, FileContent, FileName,
  FileType, Disposition, ContentId)
from sendgrid.helpers.mail import Mail

message = Mail(
  from_email='from_email@gmail.com',
  to_emails=['to_email1@gmail.com','to_email_2@gmail.com','to_email_3@gmail.com','to_email_4@gmail.com'],
  subject='Mail',
  html_content='<strong>Please find attached</strong>')
file_path = 'test.pdf'
with open(file_path, 'rb') as f:
  data = f.read()
f.close()
encoded = base64.b64encode(data).decode()
attachment = Attachment()
attachment.file_content = FileContent(encoded)
attachment.file_type = FileType('application/pdf')
attachment.file_name = FileName('test_filename.pdf')
attachment.disposition = Disposition('attachment')
attachment.content_id = ContentId('Example Content ID')
message.attachment = attachment
try:
  sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
  response = sendgrid_client.send(message)
  print(response.status_code)
  print(response.body)
  print(response.headers)
except Exception as e:
  print(e.message)
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:        18.04
Codename:       bionic


提前谢谢

您好,您的问题解决了吗?因为我目前正面临同样的问题,但使用python时:(您好,您是否设法解决了您的问题?因为我目前正面临同样的问题,但使用python时:(