hai,
i am new to php.i want to use the mailing date of sender as the reference.
for example if any one send amail on 24/12/2006 then in the reply mail
to him ishoul add a reference line as follows.
ref:this is in response to your mail dated 24/12/2006.......
how can it make possible.
email:

Dani AI

Generated

Asking for the sender's Date header is common. wanted the sending date placed into a reply and suggested regex. A reliable flow is: obtain the raw headers (IMAP/POP or piped mail), "unfold" header lines, locate the Date header (fall back to the last Received line if Date is missing), parse it with PHP's date facilities, then format the result for the reply. Regex alone can work, but use DateTime or strtotime to avoid brittle string handling.

Example: extract from raw headers, unfold and parse.

$raw = $rawHeaders;                      // raw headers string
$raw = preg_replace("/\r?\n\s+/", " ", $raw); // unfold folded headers

if (preg_match('/^Date:\s*(.+)$/mi', $raw, $m)) {
    $dateStr = trim($m[1]);
} elseif (preg_match_all('/^Received:.*;\s*(.+)$/m', $raw, $mm)) {
    $dateStr = trim(end($mm[1])); // use last Received date
} else {
    $dateStr = null;
}

if ($dateStr) {
    try {
        $dt = new DateTime($dateStr);
    } catch (Exception $e) {
        $ts = strtotime($dateStr);
        $dt = $ts ? new DateTime("@$ts") : null;
    }
    if ($dt) {
        $dt->setTimezone(new DateTimeZone('UTC')); // normalize as needed
        $replyDate = $dt->format('d/m/Y');
        $body = "ref: this is in response to your mail dated $replyDate ...";
    }
}

If messages are read from a mailbox, the IMAP helper returns a parsed date: imap_headerinfo($stream,$msgno)->date. See the extension docs for details. Be aware that Date headers can be malformed or forged; treat the value as a display field, not a trusted timestamp. For parsing and formatting, consult the PHP DateTime and strtotime references: DateTime class, strtotime, imap_headerinfo.

Recommended Answers

All 3 Replies

What is your design? Are you trying to extract the date from email headers, or are you inserting that string in the body.

Please don't expect any emails from anyone on Daniweb, the rules say that everything should stay on the board.

ya..,i have to extract the date from email headers.

Ok, provided you have the headers, I'd consider matching the date using a Regular Expression... I'd have to look up the exact syntax for the regular expression yet, but you should be able to match and extract the date.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.