我编写了一段发送通知电子邮件的代码,下面是负责发送通知的函数:
int send_mail(const char* mail_path) {
CURL *curl;
CURLcode curl_code;
FILE *message;
message = fopen(mail_path, "r");
struct curl_slist *recipients = NULL;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_USERNAME, "XXXX");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "YYYY");
curl_easy_setopt(curl, CURLOPT_URL, "ZZZZ");
recipients = curl_slist_append(recipients, "XXXX");
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "XXX");
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_READDATA, message);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_code = curl_easy_perform(curl);
curl_slist_free_all(recipients);
curl_easy_cleanup(curl);
}
return curl_code;
}当我从命令行使用它时,它工作得很好。
但是,当脚本通过远程服务器上的cron管理器执行时,它不会发送电子邮件。我可以通过服务器的cli正确地发送电子邮件。
代码似乎停止了curl_easy_perform步骤的工作,但是我不知道为什么--有什么技术可以用来调试这样的问题呢?
克伦·乔布斯也在自己发电子邮件--这可能是问题的原因吗?
编辑:
我检查了日志,似乎在cron管理器的情况下,我试图连接的google smtp服务的IP地址与直接从命令行运行的脚本的IP地址不同。cron的日志在354 Go ahead之后结束。除此之外,日志是相同的。
克伦:
* Rebuilt URL to: smtp://smtp.gmail.com:587/
* Trying 173.194.71.108...
...
> DATA
< 354 Go ahead ra7sm645322lbb.27 - gsmtp
---- stops here ----命令行:
* Rebuilt URL to: smtp://smtp.gmail.com:587/
* Trying 74.125.195.109...
...
< 354 Go ahead ex5sm4631837wib.2 - gsmtp
< 250 2.0.0 OK 1432828708 ex5sm4631837wib.2 - gsmtp
* Connection #0 to host smtp.gmail.com left intact发布于 2015-05-27 21:43:08
不,cronjobs自己发送邮件这一事实并不是代码失败的原因。
https://stackoverflow.com/questions/30493399
复制相似问题