gpt4 book ai didi

c - 如何发送带有变量声明的电子邮件

转载 作者:行者123 更新时间:2023-11-30 16:36:33 24 4
gpt4 key购买 nike

我在 libcurl 中编写了一段可以正常工作的代码,但我想进行一些更改。

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

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

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

代码:

    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

消息输出:

Message Test
Day 22 Welcome Lucas

最佳答案

我为你做了一个例子,你可以用它来实现你想要的。

要准确地获得您想要的内容,您可以更改我在下面制作的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!");
}

祝你好运,请告诉我这是否是正确的答案。我已尽力提供信息。

关于c - 如何发送带有变量声明的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48383627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com