Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct headers for replying and forwarding emails

Are you that programmer that knows all about emails? Can you dream about mail headers, write them with your eyes closed? Maybe you might be able to help me with this issue.

Let me explain ...

Workflow

  • The mail server receives emails
  • A PHP cronjob imports all emails from IMAP into the MySQL database
  • The user finds all emails in the Inbox in my application

Sending an email

In that same application a user can send new emails. I've handled that using the following code snippet.

$message = \Swift_Message::newInstance()
    ->setSubject($form->get('subject')->getData())
    ->setFrom('[email protected]')
    ->setTo($form->get('to'))
    ->setBody(
        $this->renderView(
            'MailBundle:Email:contact.html.twig',
            array(
                'ip' => $request->getClientIp(),
                'name' => $form->get('to')->getData(),
                'message' => $form->get('message')->getData()
            )
        )
    )
;

This works fine. If the above email is submitted I create a new email in the IMAP sent folder. This, on his turn is being imported by the PHP cronjob and put in the MySQL database. It shows up in the sent folder in my application.

The issue

Now that you have a general idea of how my application works I've got some things I am not so sure about.

  • How can I create replies to imported emails using SwiftMailer.
  • How can I forward imported emails using SwiftMailer

I would like to use my application as a real mailclient and want the mail headers etc. to be correctly set.

I could of course just send an email with the original mail body and the subject prepended with "RE:". But I am not sure if that's all. I somehow suspect I need to do alot more.

In short

How would I use SwiftMailer to reply to or forward an email that is saved in a database?

Update

I implemented the reply headers as suggested in an answer to this question. I'm not sure however if this will work for the forwarding as well. I just wanna make sure that mail providers won't block my emails because the headers are incorrect.

like image 641
Peter Avatar asked Nov 10 '15 13:11

Peter


1 Answers

I compared what are the differences in the header between a "first" email and a reply.

To make a reply, it seems you've to add two lines in the reply header :

  • In-Reply-To: [Message-ID] (with Message-ID found in the header of the first mail)

  • References: [Message-ID]

Obviously, you have to change the from and to in the reply email. (I think SwiftMailer creates others by itself)

To add line in email header using SwiftMailer, proceed like that :

$headers = $message->getHeaders();

$headers->addTextHeader('In-Reply-To', $previousEmail->getHeaders()->getMessageId());

This getter I put is just how I imagine your email entity.

For the forward, juste print the source of an email and check differences.

Hope it could help.


Update :

For a forward : it is exactly the same thing. I just compared the two headers.

You've to add In-Reply-To and References.

And to answer your update : You can put anything you want (string obviously) in an email header. The spam score will not grow up if you add an unknown variable in it.

like image 51
Julien Bourdic Avatar answered Oct 12 '22 02:10

Julien Bourdic