【问题标题】:Sending e-mails with curl from C++从 C++ 使用 curl 发送电子邮件
【发布时间】:2015-08-10 04:25:43
【问题描述】:

我写了一小段发送通知邮件的代码,下面是负责发送通知的函数:

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 作业也在自己创建电子邮件 - 这可能是问题的原因吗?

编辑:

我检查了日志,似乎对于 cron 管理器,我尝试连接的 google 的 smtp 服务的 IP 地址与直接从命令行运行的脚本不同。来自 cron 的日志在 354 Go ahead 之后结束。除了日志是一样的。

cron:

* 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

【问题讨论】:

    标签: c++ email curl cron


    【解决方案1】:
    1. CURLOPT_VERBOSE 切换到1L

    2. 为您的失败尝试记录标准错误并在失败后对其进行研究

    不,cronjobs 发送自己的邮件这一事实并不是此代码失败的原因。

    【讨论】:

    • 谢谢,这让我离答案更近了一点。我在检查日志后更新了我的问题,有什么想法吗?
    猜你喜欢
    • 2013-01-21
    • 2018-12-05
    • 2021-09-20
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2010-10-01
    相关资源
    最近更新 更多