Pop\Mail\Mail::saveTo PHP Method

saveTo() public method

Save mail message or messages in a folder to be sent at a later date.
public saveTo ( string $to = null, string $format = null ) : Mail
$to string
$format string
return Mail
    public function saveTo($to = null, $format = null)
    {
        $dir = null !== $to ? $to : getcwd();
        if (null === $this->message->getMessage()) {
            $this->message->init();
        }
        $messageBody = $this->message->getMessage();
        $headers = $this->buildHeaders();
        // Send as group message
        if ($this->group) {
            $email = 'To: ' . (string) $this->queue . $this->message->getEol() . 'Subject: ' . $this->subject . $this->message->getEol() . $headers . $this->message->getEol() . $this->message->getEol() . $messageBody;
            $emailFileName = null !== $format ? $format : ($emailFileName = '0000000001-' . time() . '-popphpmail');
            // Save the email message.
            file_put_contents($dir . DIRECTORY_SEPARATOR . $emailFileName, array(), $email);
        } else {
            // Iterate through the queue and send the mail messages.
            $i = 1;
            foreach ($this->queue as $rcpt) {
                $fileFormat = null;
                $subject = $this->subject;
                $message = $messageBody;
                // Set the recipient parameter.
                $to = isset($rcpt['name']) ? $rcpt['name'] . " <" . $rcpt['email'] . ">" : $rcpt['email'];
                // Replace any set placeholder content within the subject or message.
                foreach ($rcpt as $key => $value) {
                    $subject = str_replace('[{' . $key . '}]', $value, $subject);
                    $message = str_replace('[{' . $key . '}]', $value, $message);
                    if (null !== $format) {
                        if (null !== $fileFormat) {
                            $fileFormat = str_replace('[{' . $key . '}]', $value, $fileFormat);
                        } else {
                            $fileFormat = str_replace('[{' . $key . '}]', $value, $format);
                        }
                    }
                }
                $email = 'To: ' . $to . $this->message->getEol() . 'Subject: ' . $subject . $this->message->getEol() . $headers . $this->message->getEol() . $this->message->getEol() . $message;
                if (null !== $fileFormat) {
                    $emailFileName = sprintf('%09d', $i) . '-' . time() . '-' . $fileFormat;
                } else {
                    $emailFileName = sprintf('%09d', $i) . '-' . time() . '-popphpmail';
                }
                // Save the email message.
                file_put_contents($dir . DIRECTORY_SEPARATOR . $emailFileName, $email);
                $i++;
            }
        }
        return $this;
    }

Usage Example

コード例 #1
0
ファイル: MailTest.php プロジェクト: nicksagona/PopPHP
 public function testSaveTo()
 {
     $m = new Mail('Subject', array('name' => 'Bob Smith', 'email' => '*****@*****.**'), 'Hello World');
     $m->setHtml('Hello');
     $m->setText('Hello');
     $m->saveTo(__DIR__ . '/../tmp');
     $d = new Dir(__DIR__ . '/../tmp');
     $files = $d->getFiles();
     $exists = false;
     foreach ($files as $file) {
         if (strpos($file, 'popphpmail') !== false) {
             $exists = true;
             unlink(__DIR__ . '/../tmp/' . $file);
         }
     }
     $this->assertTrue($exists);
 }
All Usage Examples Of Pop\Mail\Mail::saveTo