From d8f8c7c38149a750590317b251ecec2b565848d5 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Sun, 10 Jul 2016 18:43:05 +0200 Subject: [PATCH] Moved mail sending to new class. --- index.php | 24 ++------------ lib/Frs/Output/MailOutput.php | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 21 deletions(-) diff --git a/index.php b/index.php index a26fc3a..ad5cd11 100644 --- a/index.php +++ b/index.php @@ -113,27 +113,9 @@ if (!$tpl_done && isset($_SESSION['access_token']) && $_SESSION['access_token']) $data['email_date'] = date('r'); $data = array_merge($data, $fields); $mo->addTemplateVars($data); - - $mail_html = $mo->getRenderedOutput(); - list($headers, $mailbody) = preg_split('/\r?\n\r?\n/', $mail_html, 2); - - echo '
'.$headers.'
'.$mailbody; - $header_lines = preg_split('/\r?\n/', $headers); - $header_filtered = ''; - $recipient = $data['user']['name_first'] . ' ' . $data['user']['name_last'] . ' <' . $data['user']['email'] . '>'; - $subject = '[FRS] ' . $data['action_uc'] . ' Reservation'; - foreach ($header_lines as $header_line) { - list($key, $value) = preg_split('/: /', $header_line, 2); - if (in_array(strtolower($key), array('subject', 'to'))) { - // Skip Subject and To headers as they're added by PHP - if (strtolower($key) == 'subject') { - $subject = $value; - } - continue; - } - $header_filtered .= $header_line . "\r\n"; - } - $mail_sent = mail($recipient, $subject, $mailbody, $header_filtered); + $mo->setSubject('[FRS] ' . $data['action_uc'] . ' Reservation'); + $mo->addRecipient($data['user']['email'], $data['user']['name_first'] . ' ' . $data['user']['name_last']); + $mail_sent = $mo->send(); if ($mail_sent) { echo 'Mail sent successfully.'; } else { diff --git a/lib/Frs/Output/MailOutput.php b/lib/Frs/Output/MailOutput.php index c01f384..691da8f 100644 --- a/lib/Frs/Output/MailOutput.php +++ b/lib/Frs/Output/MailOutput.php @@ -6,4 +6,63 @@ use \Frs\Output\GenericOutput; class MailOutput extends GenericOutput { + private $recipients = array(); + private $subject = ''; + + /** + * Adds a recipient to the list. + * + * @param string $address The email address to add. + * @param string $name The name to display. + */ + public function addRecipient($address, $name = '') + { + // TODO: Check $address for validity + $fullAddress = '<' . $address . '>'; + if (!empty($name)) { + $fullAddress = $name . ' ' . $fullAddress; + } + $this->recipients[] = $fullAddress; + } + + /** + * Sets the default subject for the mail. Might be + * overwritten by subject line from template. + * + * @param string $newSubject Subject to use. + */ + public function setSubject($newSubject) + { + $this->subject = $newSubject; + } + + /** + * Sends the prepared mail + * + * @return bool TRUE if mail was sent, FALSE if not. + */ + public function send() + { + $mail_html = $this->getRenderedOutput(); // contains headers + body + list($headers, $mailbody) = preg_split('/\r?\n\r?\n/', $mail_html, 2); + + $header_lines = preg_split('/\r?\n/', $headers); + $header_filtered = ''; + + foreach ($header_lines as $header_line) { + list($key, $value) = preg_split('/: /', $header_line, 2); + if (in_array(strtolower($key), array('subject', 'to'))) { + // Skip Subject and To headers as they're added by PHP + if (strtolower($key) == 'subject') { + $this->setSubject($value); + } + continue; + } + $header_filtered .= $header_line . "\r\n"; + } + $recipients = implode(', ', $this->recipients); + // TODO: Check if any recipients in the first place + $mail_sent = mail($recipients, $this->subject, $mailbody, $header_filtered); + return $mail_sent; + } }