SeamusJ
SeamusJ

Reputation: 441

Linux date command to generate email header

I am writing a bash script which creates emails.

How do I generate a valid date header using the Linux date command?

Upvotes: 2

Views: 1253

Answers (2)

ruhri
ruhri

Reputation: 11

From the man-page of date

-R, --rfc-email
        output date and time in RFC 5322 format.  Example: Mon,  14  Aug 2006 02:34:56 -0600

This circumvents the localized output, which does not conform to RFC5322. So use date --rfc-email to generate a valid date-string.

Upvotes: 1

SeamusJ
SeamusJ

Reputation: 441

An email header date looks like Fri, 6 Apr 2018 13:30:00 -0500.

With the date command, this is date +'%a, %-d %b %Y %H:%M:%S %z'.

RFC5322 says the formal specification for the format is:

    date-time       =   [ day-of-week "," ] date time [CFWS]
    day-of-week     =   ([FWS] day-name) / obs-day-of-week
    day-name        =   "Mon" / "Tue" / "Wed" / "Thu" /
                       "Fri" / "Sat" / "Sun"
    date            =   day month year
    day             =   ([FWS] 1*2DIGIT FWS) / obs-day
    month           =   "Jan" / "Feb" / "Mar" / "Apr" /
                       "May" / "Jun" / "Jul" / "Aug" /
                       "Sep" / "Oct" / "Nov" / "Dec"
    year            =   (FWS 4*DIGIT FWS) / obs-year
    time            =   time-of-day zone
    time-of-day     =   hour ":" minute [ ":" second ]
    hour            =   2DIGIT / obs-hour
    minute          =   2DIGIT / obs-minute
    second          =   2DIGIT / obs-second
    zone            =   (FWS ( "+" / "-" ) 4DIGIT) / obs-zone

Upvotes: 9

Related Questions