17

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('me@example.com')
    ->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.

Peter
  • 8,776
  • 6
  • 62
  • 95
  • You should take a look at the possible (and common) headers. – m02ph3u5 Nov 10 '15 at 14:12
  • @m02ph3u5 That's exactly what I'm looking for, the right headers and the right way to set them using SwiftMailer. Would you care to elaborate on this? – Peter Nov 10 '15 at 14:35
  • I would say this is not related to SwiftMailer at all. It is basically the SMTP protocal you mention. IMAP is not a Transport Protocol but a Mailbox Storage Accessor. Email is always delivered by SMTP. – mblaettermann Nov 10 '15 at 14:43
  • I think my question is clear. I wanna know what kind of headers to set when answering or forwarding an email and I would like to know how to do this in SwiftMailer. Nothing less, nothing more. – Peter Nov 10 '15 at 15:27
  • I edited my post to answer your update – Julien Bourdic Nov 19 '15 at 08:13

1 Answers1

13

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.

Julien Bourdic
  • 1,398
  • 1
  • 12
  • 29
  • This helps indeed, but how is it with the forwarding? – Peter Nov 18 '15 at 14:45
  • Send an email at one of your email address. Forward this email to you again and print the source of each email. You just have to compare the beginning of each email to know what is changing between these both. If I have a little bit of time today, I'll see. – Julien Bourdic Nov 19 '15 at 07:48
  • I've got mailboxA which sents message to mailboxB. I want to reply on that e-mail. I am creating new message, setting the: `In-Reply-To` and `References` headers to the long message ID (obtained from headers). Setting `from` to MailboxB, setting `to` to MailboxA. My subject is: `Re: Something to reply`. The e-mail gets sent correctly, however the destination mailbox (Gmail) doesn't link it with the original e-mail. What might be the problem? – Tom Raganowicz Dec 04 '18 at 11:58
  • Forwarding is very different from replying! See "3.4. Forwarding for Address Correction or Updating" in [RFC 5321](https://www.rfc-editor.org/rfc/rfc5321#section-3.4). – U. Windl Aug 09 '23 at 22:06