I am trying to modify a script which grabs data from a database and puts it into the message of an email which is then sent out using mailx. What I need to do is grab the date which is in the first line of the message body and use that as the date in the header. These emails get ingested by another system and the only way to search these emails properly by date is to include the date from the body as the header date. We sometimes run into an issue where the script does not run and we manually re run the emails. As a result, they get stamped with the current date/time rather than when the data was originally created. I know how to put the date in the message body and/or the subject, but I really need to just modify the header date.

  • "grab the date from the first line" or "modify the header date"? In your description it's unclear how your data is actually available for processing. Is your data in a file and you want to extract the significant date from the first line in the file (sed "1q") or do you want to replace the date in the first line in the file by the actual date (sed "1s/.*/$(date)/")?
    – Janis
    Mar 11 '15 at 0:06
  • The data is being pulled from a mySql database and is a series of instant messages that get aggregated by conversation into the message body. The first line in that body of text contains the date and time the conversation started. So assuming I can pull that value into a variable, I'm hoping I can use that variable to modify the header information for the email date. Mar 11 '15 at 0:13

If you stored the extracted database data in a file before you send it per mail you can use something along

mailx -s "$( sed 1q yourfile )" user@domain < yourfile
  • Not sure I follow. this looks like you are pulling the value from my file and putting it in the subject as it follows '-s'. I need to modify the time the email is sent which I understand to be a header value. I was wondering if I needed to use '-S' and then define the variable to change and the value to assign it. Mar 11 '15 at 0:30
  • To be clear, let's say I have an email I am sending out today. I want to be able to state that it was actually sent on 1/1/99. Let's assume I have the value 1/1/99 stored somewhere. I just want to focus on getting that value into the email header so the recipient sees the email as being sent on 1/1/99. I'm not worried about how to grab the value to use. Mar 11 '15 at 0:36
  • Oh, you want to "trick" the email timestamp? You'd then have to edit the raw mail, set in the header block the Date: ... field, and send the resulting file. (In former days ISTR that you could send raw mails with the mail program (as opposed to mailx) but now my man page seems to suggest that mail and mailx are equivalent programs.) Beyond those hints I cannot help further, I fear.
    – Janis
    Mar 11 '15 at 0:39
  • Thanks, yes that's why I am trying to do. Thanks for trying. I'll keep searching. I gather I could form the header info along with the body details and send that out, but I'm not that savvy to understand how to send the raw file as the entire email. Mar 11 '15 at 3:17
  • The old mail program had to be fed with the whole raw mail like you feed the mailx with the mail body, from stdin. Just keep a blank line between mail header and body. One more idea that came to my mind; this may sound strange in the first moment but you can use telnet to send raw mails. For details search for keywords "telnet send mail", or here's a link for the start: spamsoap.com/…
    – Janis
    Mar 11 '15 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.