SMTP save sent mail to INBOX.Sent

Hi, I am sending emails via the $kirby->email() helper and an smtp setting. What I’d like to do now is save the sent emails in the “sent” folder of that smtp account. A possible solution is here: smtp - Mail sent with PHP mail are not shown in my mails Sent folder - Stack Overflow .
The $kirby->email() phpmailer wrapper does not support the required getSentMIMEMessage() method nor does it allow access to the phpmailer object post send.

Has anyone done this with Kirby?

When you send an email message, you use an SMTP server, and the email commands that Kirby issues are contacting and sending the message to that server. That is also what the PHPMailer library - which Kirby uses under the hood - does: it allows us to send more complex commands to our SMTP servers, making it easier for us to send HTML emails, attachments, and more.

The server that receives a message is a different server - usually an IMAP server - and your ‘mailboxes’ (or ‘folders’) are something that only the IMAP server knows about. So, keeping that in mind, what you want to do is this:

  1. send a message (via your SMTP server) - Kirby has functions to help you do that
  2. once the message is sent, contact your IMAP server and then store a copy of that message into a specific mailbox (e.g., ‘Sent’).

Neither Kirby nor PHPMailer has an ‘easy function’ to do #2, because Kirby’s (and PHPMailer) commands are just for talking to your SMTP server and sending messages - not for receiving or manipulating messages that are stored in your IMAP mailboxes.

That doesn’t mean you can’t do that directly. The link you posted from StackOverflow shows an example of what you need to do in order to do what you want:

  1. You need to know the exact path of the mailbox on the server
  2. You then use imap_open() to open a connection to the IMAP server
  3. You then use imap_append() to ‘append’ the message to your mailbox
  4. Finally, you use imap_close() to close the connection to the IMAP server

There may be other non-programmatic ways to achieve the same result, too. For example, you could try this:

  1. Add a prefix to the ‘subject’ field of every automatic email message that is sent - like, for example, “[Automated Message]”
  2. Whenever you send a message, CC or BCC yourself (or the automated sender account)
  3. In the IMAP server itself, setup a filter in your account (or in the automated sender account) that looks for that prefix in the subject of every message arriving, and automatically moves it to the ‘Sent’ folder (or any folder you’d like).

You can usually set this up quite easily on any server directly - for example, if your email server has a cPanel interface, login and then go to ‘User Filters’. You can set all of this up right there.

@kirbyzone thank you for your explanations.
The point I was stuck at, was that Kirby’s Email handler did not allow access to the sent message (raw, including all headers). I solved it by extending the \Kirby\Email\Email class and implementing a method that exposes the required functionality.