开发者

Easy Way To Send HTML Formatted Email in C++

I have an HTML form that currently takes the inputs and sends them out in an email with HTML formatting, so that the email looks basically like the form webpage, but with all of the fields filled in.

<form method="post" action="/cgi-bin/perlscript.pl" enctype="x-www-form-encoded" name="Form">
   <input type="text" name="txtMyText" id="txtMyText" />
</form>

The post-action script is written in Perl, and I am currently converting it to C++ simply because it is much easier for me to read and maintain that way. Also, I think it is more flexible for future additions.

In Perl, I was able to use "SendMail" to send the email, and I could do something like this:

sub PrintStyles
{
print MAIL <<ENDMAIL
<html>
    <head>
        <style>
        h1.title { color: Red; }
        h3.title { color : Black; background-color: yellow; }
        </style>

<!-- Put any valid HTML here that you want -->
<!-- You can even put variables ($xxxx) into the HTML as well, like this: -->
                <td>$myVariable</td>

ENDMAIL
}

What was nice about that, was I could literally copy-and-paste my entire CSS and HTML files (very lengthy) as long as they were in-between the "ENDMAIL" tags, and they would show up perfectly. I could even put the variables in there without having to do any extra work.

My questions is: Is there a C++ library that has similar functionality? 开发者_如何学C I really don't think I can afford to do something like this:

cout << "<html>" << endl;
cout << "<head>" << endl;
cout << "......" << endl;

I'd like it to be fairly light-weight.

Thanks.


The easiest way I know of is to use SMTPClientSession class from POCO C++ Libraries. Here is a good example.


You could define the text as a const char * which would ease the pain and suffering of outputting each line via cout:

const char email_text[] =
"<html>\n"
"<head>\n"
"....";

cout.write(email_text, sizeof(email_text) - 1);
cout.flush();

std::string email_string(email_text);
cout << email_text;
cout.flush();

I haven't used the library, but my guess is that you will need to pass it either an std::string or a char *.


You might consider mimetic


C++ does not support here documents.
You will need to use a string and send it to the stream you want:

void PrintStyles(ostream& mailstream)
{

    mailstream <<  
    "<html>\n"
    "    <head>\n"
    "        <style>\n"
    "        h1.title { color: Red; }\n"
    "        h3.title { color : Black; background-color: yellow; }\n"
    "        </style>\n"
    "\n"
    "<!-- Put any valid HTML here that you want -->\n"
    "<!-- You can even put variables (" << xxxx << ") into the HTML as well, like this: -->\n"
    "                <td>" << myVariable << "</td>\n"
    "\n"
    "\n";
}

Were you get mail stream from will depend on what email package you are using.


Thank you all for the responses. I have decided to simply call the Perl script from my code, and send the response data as an argument. I know it's probably not the best solution, but I don't think my C++ options were worth it.

// Retrieve the POST data    
char* contentLength = getenv{"CONTENT_LENGTH"};
int contentSize     = atoi(contentLength);
char* contentBuffer = (char*)malloc(contentSize);
fread(contentBuffer, 1, contentSize, stdin);
string data         = contentBuffer;

// Execute "sendmail.pl" script
string perlFile     = "sendmail.pl";
string command      = "perl " + perlFile + " \"" + data + "\"";
system(command.c_str());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜