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:
- send a message (via your SMTP server) - Kirby has functions to help you do that
- 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:
- You need to know the exact path of the mailbox on the server
- You then use imap_open() to open a connection to the IMAP server
- You then use imap_append() to ‘append’ the message to your mailbox
- 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:
- Add a prefix to the ‘subject’ field of every automatic email message that is sent - like, for example, “[Automated Message]”
- Whenever you send a message, CC or BCC yourself (or the automated sender account)
- 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.