punch_card_don has asked for the wisdom of the Perl Monks concerning the following question:

Meridian Monks,

I've got a nice little Sendmail script, the basic, classic

$mailprog = '/usr/sbin/sendmail'; $to = "you@yourdomain.com"; $from="me@mydomain.com" $subject = "something"; $content = "bla bla bla"; open(MAIL,"|$mailprog -t"); print MAIL "MIME-Version: 1.0\n"; print MAIL "Content-Type: text/html; charset=us-ascii\n"; print MAIL "To: $to\n"; print MAIL "From: $from\n"; print MAIL "Subject: $subject\n"; print MAIL "$content\n"; close (MAIL);
And it works real fine.

Except for one thing: it gives you no record you can keep of your email's expedition. There's no "Sent" folder in your email client for Sendmail emails.

Other than Cc'ing yourself, is there any way to consult the outgoing email server and create something that confirms what was sent to whom when containing what?

Thanks.




Forget that fear of gravity,
Get a little savagery in your life.

Replies are listed 'Best First'.
Re: Create "Sent"-like reference for Sendmail mails.
by cosmicperl (Chaplain) on Oct 19, 2007 at 01:35 UTC
    Write your own log where ever, in what ever format you want... Just add some extra Perl code to do it.

    My most important advice to you is that if the input for $to, $from or $subject comes from a users input (i.e. a HTML form of a browser) make sure you check it's valid!!! Epecially check for ; : , or \r and \n. Otherwise you'll find that hackers will end up using your script to send out bulk email and you'll get all sorts of problems from you web host.
    For example, they'll fill the $to field with multiple email addresses using ; , or :. Even if you check for those, and think your body text is fixed, you'll use \r and \n to add their own headers that'll disregard yours and send out what ever emails they want to whoever they want!
    Take heed of that otherwise you'll end up regretting it!

    Lyle
Re: Create "Sent"-like reference for Sendmail mails.
by gamache (Friar) on Oct 19, 2007 at 01:02 UTC
    You can get everything but the "containing what" part from the mail server logs, if the server is set up appropriately. If you need a record of the data sent, the cheapest solutions are to store it with perl (text log, tied hash, DBI... sky's the limit here), or just bcc: an appropriate email address and catch it there.
Re: Create "Sent"-like reference for Sendmail mails.
by punch_card_don (Curate) on Oct 19, 2007 at 13:59 UTC
    Write my own log - not bad. 'Cause this is on a shared server and the odds of me getting access to the raw email server logs are....

    BUT - Before I make an entry in my home-made log, I need to know that the message was actually sent by the email server. I'm not talking about receipt by the other party, but does Sendmail return some sort of confirmation that at least the email was in fact dispatched from the outgoing email server? I need somthing more than "my script processed this request". I need "the outgoing email server confirms that this request was received from my script and the email dispatched". Or at the very least, "this request was successfully submitted to the outgoing email server and the email is in queue to be dispatched".

      Sendmail, set up properly, will log the status of a mail it tried to send. An experienced email admin can read the log and tell you what the status of your file is, whether it was sent, or is waiting to be sent, etc. So could an appropriately written Perl script.

      What sendmail cannot do is guarantee the integrity of the file once transmitted. If the receiving server decides to eat the file on the other end, that can't be logged.

      That the SMTP protocol doesn't support "guaranteed delivery" is pretty easy to see. The Wikipedia article on SMTP shows a manual SMTP session. And any protocol whose basic outbound steps are:

      HELO (or EHLO)
      MAIL FROM
      RCPT TO
      DATA
      QUIT

      Doesn't have a whole lot of room for, "Oh yeah, I just sent the file you sent me." Understand that SMTP was written in the days of expensive communications, when dialup was common. These servers had to live with imprecise delivery and often longish delays. Servers weren't wired to one another then.

      Update: SMTP doesn't guarantee delivery
Re: Create "Sent"-like reference for Sendmail mails.
by talexb (Chancellor) on Dec 16, 2007 at 22:59 UTC

    I would say your best bet is a CC (or BCC) to a 'log E-Mail' address, assuming that this message doesn't contain proprietary or confidential information.

    You'll get a complete copy of the E-Mail, and you'll be able to store it somewhere safe for later retrieval and analysis.

    Way easier than writing your own logging code. ;) But that's just my preference.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds