Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.



C++
#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;
}
Posted
Updated 24-Nov-14 23:18pm
v3

1 solution

You add variables exactly the same as you add static values, by writing the variable names in the code.
 
Share this answer
 
Comments
Member 10896619 24-Nov-14 5:25am    
I already done this but fail to send mail....
Richard MacCutchan 24-Nov-14 5:46am    
You know an answer like that really does not help us to help you. If something does not work then please provide complete details; use the Improve question link to edit your post, and explain clearly what your problem is.
Member 10896619 25-Nov-14 5:19am    
Sir, I improved my question please help.
Richard MacCutchan 25-Nov-14 5:22am    
Help with what? You need to explain exactly where your code does not work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900