Re: how can I send the error message to the user by email?



DaVinci wrote:
Bill Pursell wrote:
DaVinci wrote:
how can I send the error message to the user by email?
such as
void sendmail(std::string errormsg ,std::string email)
{

}

Probably the easiest way would be:

void
sendmail(char *email_address, char *subject, char *message)
{
char t[BUFSIZ];
snprintf(t, BUFSIZ, "echo \"%s\" | mail -s %s %s",
message, subject, email_address);
system(t);
}

Yes,it works ok.but how it work?

The short answer is:
% man mail
% man system

The long answer is:
Mail reads stdin and sends the contents to the specified
user with the specified subject. System spawns a shell
and sends the contents of the message (via echo) to the
input stream of an invocation of mail. As previously
pointed out in this thread, this is extremely dangerous,
as anyone who can manipulate the message content
can use the system() call to execute arbitrary instructions.
This is probably the reason that popen() was suggested.

However, I think there is an overall better approach. Namely,
this probably shouldn't be done in C++. I have never
had a reason to use system(). Any time it would be
appropriate, it is more appropriate to re-write the application
in a higher level language, like Python. You might consider
having your code dump its error message to a log file, and have
some other process handle the e-mailing of the
error log (for example, the parent of the process
that is having errors). I say this because system() is a
resource hog, and if you are willing to use it then performance
is obviously not an issue in that section of code. If performance
isn't a concern, then use Python.

.



Relevant Pages

  • Re: Does Python really follow its philosophy of "Readability counts"?
    ... We're told Python doesn't have private attributes. ... the advice to not use getters/setters for no good reason, given Python's support for computed attributes - which is not exactly the same thing. ... stricter, with enforced data hiding, the objections come thick and fast: people vehemently say that they like Python just the way it is, that they want the ability to mess with the internals. ...
    (comp.lang.python)
  • Python and stale file handles
    ... I started back programming Python again after a hiatus of several ... regardless of how hard I try- it it starts with tailing a log file. ... I have a shell script constantly writes to the logfile.log... ... #Set the filename and open the file ...
    (comp.lang.python)
  • Re: Does Python really follow its philosophy of "Readability counts"?
    ... want the ability to mess with the internals. ... There's no shortage of B&D languages, and well, Python is OSS, ... leave my favorite language alone. ... always believe that their reason is a good reason. ...
    (comp.lang.python)
  • Re: Hows ruby compare to it older brother python
    ... >>to Perl than to Python. ... > as syntax and Ruby doesn't. ... As I said in another post, indentation is the reason I learned ...
    (comp.lang.python)
  • Re: default value in __init__
    ... Of course there's no reason anyone would bother ... official Python Reference Manual to see whether they're explicit ... There's a section titled "7.6 Function definitions". ... book - the book pointed out the "surprise" you get when you say ...
    (comp.lang.python)