er_web-pr
er_web-pr

Reputation: 55

Reply to an email using SMTP

The idea of my project is to retrieve Emails from Gmail using IMAP and store it in database.
Than send new messages using PHPMailer. My problem is how can i reply to an email that's exist in my database, and what information do i need to reply to an email . This is the code i'm using to send emails . but i can't reply to an email . when ever i want to reply to an email i find that i just send new one . This is just the model

public function send_email_reply($from, $to, $subject, $message)
    {
        $body =
            '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>'.htmlspecialchars($subject, ENT_QUOTES, $this->email->charset).'</title>
                <style type="text/css">
                    body {
                        font-family: Arial, Verdana, Helvetica, sans-serif;
                        font-size: 16px;
                    }
                </style>
            </head>
            <body>
            '.$message.'
            </body>
            </html>';
            $body = $this->email->full_html($subject, $message);
            $result = $this->email
                ->from($from)
                ->to($to)  
                ->subject($subject)
                ->message($body)
                ->send();

        return $result;
    } 

Upvotes: 1

Views: 4202

Answers (1)

MCToon
MCToon

Reputation: 668

When an email client replies to a message it actually is a new message. However, the message looks like a reply and will get associated with the original message because of careful construction on the part of the mail client generating the reply.

To make it look like a reply the text of the original message gets included typically indented with a vertical bar in front of each line. For the plain text part of the message each line is prepended with a single angle bracket, this is not too difficult. For the HTML part the client has to construct something that will render properly, this can be quite difficult.

From your code sample I can't tell how you are structuring the message, but it's probably not taking these factors into account. You will need to analyze the MIME structure of the original message, grab the TEXT/PLAIN part and construct a proper looking TEXT/PLAIN reply, then you need to do the same with the TEXT/HTML part. Once you have the properly structured parts you need to construct a new MIME message with your TEXT/PLAIN and TEXT/HTML parts.

There are several PHP MIME parsers, I don't have a specific recommendation, we actually use several depending on need including one we wrote in-house. php-mime-mail-parser is pretty good.

To make your message act like a reply you need to put in a couple headers correctly. Take the Message-ID of the original message and put it in the In-Reply-To: header on the new message. Get the References: header from the original message and append the original Message-ID to the end of the list, separate different Message-IDs with a single space. If it didn't exist previously, create a new one with just the original Message-ID.

If the References: header gets long you may want to truncate entries. I don't know of a specific limit, in our implementations we have not imposed any limitations. Just to be sure to fold lines to be no more than 998 characters.

The In-Reply-To: and References: headers for replies are specified in RFC 5322 sec 3.6.4.

For a more lengthly discussion of how threading works see Jamie Zawinski's work on email threading, it has been extremely useful in our email threading analysis for our E-Discovery tools. It is dated, but still relevant.

Upvotes: 4

Related Questions