public function init()
{
$msgType = $this->getMessageType();
if (null === $msgType) {
throw new Exception('Error: The message body elements are not set.');
}
if (count($this->mail->getQueue()) == 0) {
throw new Exception('Error: There are no recipients for this email.');
}
$this->message = null;
$this->setBoundary();
switch ($msgType) {
// If the message contains files, HTML and text.
case self::TEXT_HTML_FILE:
$this->mail->setHeaders(array('MIME-Version' => '1.0', 'Content-Type' => 'multipart/mixed; boundary="' . $this->getBoundary() . '"' . $this->eol . "This is a multi-part message in MIME format."));
$attachments = $this->mail->getAttachments();
foreach ($attachments as $attachment) {
$this->message .= $attachment->build($this->getBoundary(), $this->eol);
}
$this->message .= '--' . $this->getBoundary() . $this->eol . 'Content-type: text/html; charset=' . $this->getCharset() . $this->eol . $this->eol . $this->html . $this->eol . $this->eol;
$this->message .= '--' . $this->getBoundary() . $this->eol . 'Content-type: text/plain; charset=' . $this->getCharset() . $this->eol . $this->eol . $this->text . $this->eol . $this->eol . '--' . $this->getBoundary() . '--' . $this->eol . $this->eol;
break;
// If the message contains files and HTML.
// If the message contains files and HTML.
case self::HTML_FILE:
$this->mail->setHeaders(array('MIME-Version' => '1.0', 'Content-Type' => 'multipart/mixed; boundary="' . $this->getBoundary() . '"' . $this->eol . "This is a multi-part message in MIME format."));
$attachments = $this->mail->getAttachments();
foreach ($attachments as $attachment) {
$this->message .= $attachment->build($this->getBoundary(), $this->eol);
}
$this->message .= '--' . $this->getBoundary() . $this->eol . 'Content-type: text/html; charset=' . $this->getCharset() . $this->eol . $this->eol . $this->html . $this->eol . $this->eol . '--' . $this->getBoundary() . '--' . $this->eol . $this->eol;
break;
// If the message contains files and text.
// If the message contains files and text.
case self::TEXT_FILE:
$this->mail->setHeaders(array('MIME-Version' => '1.0', 'Content-Type' => 'multipart/mixed; boundary="' . $this->getBoundary() . '"' . $this->eol . "This is a multi-part message in MIME format."));
$attachments = $this->mail->getAttachments();
foreach ($attachments as $attachment) {
$this->message .= $attachment->build($this->getBoundary(), $this->eol);
}
$this->message .= '--' . $this->getBoundary() . $this->eol . 'Content-type: text/plain; charset=' . $this->getCharset() . $this->eol . $this->eol . $this->text . $this->eol . $this->eol . '--' . $this->getBoundary() . '--' . $this->eol . $this->eol;
break;
// If the message contains HTML and text.
// If the message contains HTML and text.
case self::TEXT_HTML:
$this->mail->setHeaders(array('MIME-Version' => '1.0', 'Content-Type' => 'multipart/alternative; boundary="' . $this->getBoundary() . '"' . $this->eol . "This is a multi-part message in MIME format."));
$this->message .= '--' . $this->getBoundary() . $this->eol . 'Content-type: text/plain; charset=' . $this->getCharset() . $this->eol . $this->eol . $this->text . $this->eol . $this->eol;
$this->message .= '--' . $this->getBoundary() . $this->eol . 'Content-type: text/html; charset=' . $this->getCharset() . $this->eol . $this->eol . $this->html . $this->eol . $this->eol . '--' . $this->getBoundary() . '--' . $this->eol . $this->eol;
break;
// If the message contains HTML.
// If the message contains HTML.
case self::HTML:
$this->mail->setHeaders(array('MIME-Version' => '1.0', 'Content-Type' => 'multipart/alternative; boundary="' . $this->getBoundary() . '"' . $this->eol . "This is a multi-part message in MIME format."));
$this->message .= '--' . $this->getBoundary() . $this->eol . 'Content-type: text/html; charset=' . $this->getCharset() . $this->eol . $this->eol . $this->html . $this->eol . $this->eol . '--' . $this->getBoundary() . '--' . $this->eol . $this->eol;
break;
// If the message contains text.
// If the message contains text.
case self::TEXT:
$this->mail->setHeaders(array('Content-Type' => 'text/plain; charset=' . $this->getCharset()));
$this->message = $this->text . $this->eol;
break;
// Else if nothing has been set yet
// Else if nothing has been set yet
default:
$this->message = null;
}
return $this;
}
/** * Save mail message or messages in a folder to be sent at a later date. * * @param string $to * @param string $format * @return \Pop\Mail\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; }