如何使用变量声明发送电子邮件

时间:2018-01-22 14:18:52

标签: c libcurl

我在libcurl中生成的代码正常工作,但我想做一些更改。

我想知道如何在消息中使用声明变量发送消息。

要进行此更改,我认为我不会使用数组,而是必须更改大部分代码。

我不知道如何最好地做到这一点,有没有人知道如何最好地做到这一点?

代码:

Pause the execution for cq5 to come up

我想如何发送消息的示例:

    typedef struct
{
    int lines_read;
} UploadStatus;

size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
{
    if((size == 0) || (nmemb == 0) || ((size * nmemb) < 1)) return 0;

    UploadStatus *upload = (UploadStatus*) userp;
    const char *payload[] =
    {
        "Message Test\r\n",
        "\r\n", /* empty line to divide headers from body, see RFC5322 */
        "Test",
        NULL
    };
    const char *data = payload[upload->lines_read];

    if(data)
    {
        size_t len = strlen(data);
        memcpy(ptr, data, len);
        upload->lines_read++;
        return len;
    }
    return 0;
}

int main(void)
{
    CURLcode res = CURLE_OK;
    struct curl_slist *recipients = NULL;
    UploadStatus upload_ctx = {0};
    const char *from = "test@gmail.com";
    const char *to = "UrTxtEmail"; // see http://www.emailtextmessages.com/
    CURL *curl = curl_easy_init();

    if(curl)
    {
        // set username and password
        curl_easy_setopt(curl, CURLOPT_USERNAME, from);
        curl_easy_setopt(curl, CURLOPT_PASSWORD, "password");
        curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.gmail.com:587/");
        // start with normal connection, and upgrade to TLS using STARTTLS
        curl_easy_setopt(curl, CURLOPT_USE_SSL, (long) CURLUSESSL_ALL);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
        recipients = curl_slist_append(recipients, to);
        curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
        curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
        // useful for debugging encryped traffic
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        // send the message
        res = curl_easy_perform(curl);
        if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        curl_slist_free_all(recipients);
        curl_easy_cleanup(curl);
    }

    return (int) res;
}

消息输出:

time_t clock;
struct tm *tm;
time(&clock);
tm = localtime(&clock);

"Message Test\r\n",
"\r\n", /* empty line to divide headers from body, see RFC5322 */
"Day %d Welcome %s", tm->tm_mday, user_name

1 个答案:

答案 0 :(得分:0)

我为你做了一个例子,你可以用它来达到你想要的目的。

要准确得到您想要的内容,您可以将我在下面创建的payload_template更改为类似

const char payload_template[] =
        "Date: %s\r\n"
        "To: %s\r\n"
        "From: %s\r\n"
        "Message-ID: <%s@example.com>\r\n"
        "Subject: Message Test\r\n"
        "\r\n"
        "Day %d Welcome %s\r\n\r\n";

随后修改了sprintf()

sprintf(payload_text, payload_template, time_buffer, to,
        smtp_from, message_id, tm->tm_mday, user_name);

如果要使用该示例,则必须添加生成tm结构的代码以及user_name。 以下是基于libcurl示例的完整示例。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>

struct upload_status {
    const char *readptr;
    size_t sizeleft;
};

static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
{
  struct upload_status *upload_ctx = (struct upload_status *)userp;
  const char *data;

  size_t bytes_to_copy = upload_ctx->sizeleft < size*nmemb ? upload_ctx->sizeleft : size*nmemb;
  bytes_to_copy = upload_ctx->sizeleft < 10 ? upload_ctx->sizeleft : 10;

  if(size*nmemb < 1)
    return 0;

  if(upload_ctx->sizeleft)
  {
    memcpy((char*)ptr, (char*)upload_ctx->readptr, bytes_to_copy);
    upload_ctx->readptr+=bytes_to_copy;
    upload_ctx->sizeleft-=bytes_to_copy;
    return bytes_to_copy;
  }

  return 0;
}

int send_mail(const char* to, const char* message)
{
    CURL *curl;
    CURLcode res = CURLE_OK;
    struct curl_slist *recipients = NULL;
    struct upload_status upload_ctx;

    // TODO Change these
    const char* smtp_from       = "mr.christer@example.com";
    const char* smtp_username   = "mr.christers_username";
    const char* smtp_password   = "secret";
    const char* smtp_server_url = "smtp://mail.example.com:25";


    curl = curl_easy_init();
    if(curl)
    {
        // Time
        time_t rawtime;
        struct tm * timeinfo;
        char time_buffer[128];
        time(&rawtime);
        timeinfo = localtime (&rawtime);
        strftime (time_buffer, 128, "%a, %d %b %Y %H:%M:%S %z", timeinfo);
        // END Time

        // This is what our message will look like, but with details filled in.
        const char payload_template[] =
        "Date: %s\r\n"
        "To: %s\r\n"
        "From: %s\r\n"
        "Message-ID: <%s@example.com>\r\n"
        "Subject: Hello from Mr.Christer at Stackoverflow\r\n"
        "\r\n"
        "%s\r\n\r\n";

        // TODO You must make this unique for every message sent.
        // Generate it according to spec.
        char message_id[] = "126cfbe1fd5413ba4d604c50a74bfc80471cec367b1604ade4d081f31c3f4f34";

        size_t payload_text_len = strlen(payload_template) +  
                                  strlen(time_buffer) +
                                  strlen(to) + strlen(smtp_from) +
                                  strlen(message_id) + strlen(message) + 1;

        char* payload_text = malloc(payload_text_len);

        memset(payload_text, 0, payload_text_len);

        sprintf(payload_text, payload_template, time_buffer, to,
                smtp_from, message_id, message);

        upload_ctx.readptr = payload_text;
        upload_ctx.sizeleft = (long)strlen(payload_text);

        /* Set username and password */
        curl_easy_setopt(curl, CURLOPT_USERNAME, smtp_username);
        curl_easy_setopt(curl, CURLOPT_PASSWORD, smtp_password);

        curl_easy_setopt(curl, CURLOPT_URL, smtp_server_url);

        // If you are not using SSL/TLS you are safe commenting three lines below
        curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, smtp_from);

        recipients = curl_slist_append(recipients, to);

        // More recipients
        //recipients = curl_slist_append(recipients, CC);
        curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);

        curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
        curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        /* Send the message */
        res = curl_easy_perform(curl);

        /* Check for errors */
        if(res != CURLE_OK)
          printf("curl_easy_perform() failed %s\n", curl_easy_strerror(res));

        /* Free the list of recipients */
        curl_slist_free_all(recipients);

        /* Always cleanup */
        curl_easy_cleanup(curl);

        free(payload_text);
  }

  return (int)res;
}

int main(void)
{
        return send_mail("friend@example.com", "Hello friend!");
}
祝你好运,如果这是正确答案,请告诉我。鉴于这些信息,我尽力了。