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.
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.
@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.