Search code examples
phpphpmailer

PHP Mailer compile and store message for later sending


Can we compile and store messages as a draft in PHP mailer and then later on send each message?


Solution

  • It is possible to do this with PHPMailer. After configuring the PHPMailer instance with everything you'd normally need to send the message, do not call send() - instead call preSend() (which constructs the message) and then get the message content using getSentMIMEMessage(), e.g.

    $mail->preSend();
    $message = $mail->getSentMIMEMessage();
    

    $message will then contain a complete RFC822 message which you can stick in a database, queue or whatever to send later. To do the actual sending later, you could make use of PHPMailer's SMTP class directly. To see how to drive that class, look at PHPMailer's smtpSend() function.