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

  • 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: 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)
  • Re: Why do Pythoneers reinvent the wheel?
    ... In my opinion this has got more to deal with the open source vs. ... Pythoneers reinvent the wheel? ... this tendency in also the same reason why this language is so much ... So, without taking anything out of python, I'm wondering if a richer ...
    (comp.lang.python)
  • Re: Attack a sacred Python Cow
    ... The idea that Python behaves this way is new to me. ... To many people previously familiar with OO programming in other ... Furthermore, as you described, defining the function within the scope ... No, that's not the reason. ...
    (comp.lang.python)