如何在c ++中使用curl添加动态邮件正文 [英] How to add dynamic mail body using curl in c++
本文介绍了如何在c ++中使用curl添加动态邮件正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,朋友我正在使用以下代码发送邮件,但这里邮件的主体是静态定义的,但我想发送一些变量值,如UserNamemail和Passwordmail。在这里,如果我声明UserNamemail =sljfgsg,Passwordmail =lksdhfi然后我的邮件成功,但我的这些变量的值将会改变,所以我不能不断声明,所以请帮助我如何在我的邮件正文中添加我的变量。
#define FROM< sender@example.org>
#define TO< addressee@example.net>
#define CC< info@example.org>
static const char * payload_text [] = {
日期:星期一,2010年11月29日21 :54:29 + 1100 \\\\ n,
要: TO \\\\ n,
From: FROM (示例用户)\ r\ n,
Cc : CC (另一个示例用户)\ r \ n,
消息ID:< dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org> \\ n,
主题:SMTP TLS示例消息\\\\ n,
\\\\ n, / * 用于从标题中划分标题的空行,请参阅RFC5322 * /
您的XXXXX演示请求已成功接受。\\\\ n,
您的登录凭据如下 - .\\ n,
\\\\ n,
UserName is - ,UserNamemail, \\\\ n,
密码为 - ,Passwordmail, \\\\ n,
\\\\ n,
感谢您对XXXX感兴趣。 \\\\ n,
\\\\ n,
它可能是很多行,可以是MIME编码的,无论如何。\ r \ n,
检查RFC5322.\r\ n,
NULL
};
struct upload_status {
int lines_read;
};
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;
if ((size == 0 )||(nmemb == 0 )||((size * nmemb)< 1 )){
return 0 ;
}
data = payload_text [upload_ctx-> lines_read];
if (data){
size_t len = strlen(数据);
memcpy(ptr,data,len);
upload_ctx-> lines_read ++;
return len;
}
return 0 ;
}
int main( void )
{
CURL * curl;
CURLcode res = CURLE_OK;
struct curl_slist * recipients = NULL;
struct upload_status upload_ctx;
upload_ctx.lines_read = 0 ;
curl = curl_easy_init();
if (curl){
/ * 设置用户名和密码* /
curl_easy_setopt(curl,CURLOPT_USERNAME, user 跨度>);
curl_easy_setopt(curl,CURLOPT_PASSWORD, secret);
curl_easy_setopt(curl,CURLOPT_URL, smtp://mainserver.example.net:587 跨度>);
curl_easy_setopt(curl,CURLOPT_USE_SSL,( long )CURLUSESSL_ALL);
curl_easy_setopt(curl,CURLOPT_CAINFO, / path / to / certificate.pem跨度>);
curl_easy_setopt(curl,CURLOPT_MAIL_FROM,FROM);
recipients = curl_slist_append(recipients,TO);
recipients = curl_slist_append(收件人,CC);
curl_easy_setopt(curl,CURLOPT_MAIL_RCPT,收件人);
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);
/ * 发送消息* /
res = curl_easy_perform (卷曲);
/ * 检查错误* /
if (res!= CURLE_OK)
fprintf(stderr, curl_easy_perform()失败:%s \ n,
curl_easy_strerror(res));
/ * 免费收件人列表* /
curl_slist_free_all (收件人);
/ * 始终清理* /
curl_easy_cleanup(curl) ;
}
return ( int )res;
}
解决方案
通过在代码中写入变量名称,添加与添加静态值完全相同的变量。
Hello, Friends I am using following code to send a mail but here the body of mail is statically defined but i want to send some variable values like UserNamemail and Passwordmail. Here if i declare UserNamemail="sljfgsg",Passwordmail="lksdhfi" then my mail going successfully but my value for these variables will be change so i can not declare constantly,so please help me how to add my variable in my mail body.
#define FROM "<sender@example.org>"
#define TO "<addressee@example.net>"
#define CC "<info@example.org>"
static const char *payload_text[] = {
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
"To: " TO "\r\n",
"From: " FROM "(Example User)\r\n",
"Cc: " CC "(Another example User)\r\n",
"Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\r\n",
"Subject: SMTP TLS example message\r\n",
"\r\n", /* empty line to divide headers from body, see RFC5322 */
"Your request for XXXXX Demo is successfully accepted.\r\n",
"Your Login Credentials are as follows-.\r\n",
"\r\n",
"UserName is - ",UserNamemail,"\r\n",
"Password is - ",Passwordmail,"\r\n",
"\r\n",
"Thanks for making interest in XXXX. \r\n",
"\r\n",
"It could be a lot of lines, could be MIME encoded, whatever.\r\n",
"Check RFC5322.\r\n",
NULL
};
struct upload_status {
int lines_read;
};
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;
if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
return 0;
}
data = payload_text[upload_ctx->lines_read];
if(data) {
size_t len = strlen(data);
memcpy(ptr, data, len);
upload_ctx->lines_read++;
return len;
}
return 0;
}
int main(void)
{
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *recipients = NULL;
struct upload_status upload_ctx;
upload_ctx.lines_read = 0;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
curl_easy_setopt(curl, CURLOPT_URL, "smtp://mainserver.example.net:587");
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
recipients = curl_slist_append(recipients, TO);
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)
fprintf(stderr, "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);
}
return (int)res;
}
解决方案
You add variables exactly the same as you add static values, by writing the variable names in the code.
这篇关于如何在c ++中使用curl添加动态邮件正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文