/** * @see \wcf\system\mail\MailSender::sendMail() */ public function sendMail(Mail $mail) { if (MAIL_USE_F_PARAM) { return @mb_send_mail($mail->getToString(), $mail->getSubject(), $mail->getBody(), $mail->getHeader(), '-f' . MAIL_FROM_ADDRESS); } else { return @mb_send_mail($mail->getToString(), $mail->getSubject(), $mail->getBody(), $mail->getHeader()); } }
/** * _prepMessageData * Takes the mail message and returns a url friendly querystring * @param Mail $mail [description] * @return String - the data query string to be posted */ protected function _prepMessageData(Mail $mail) { /* the api expects a 'to' parameter, but this parameter will be ignored * since we're sending the recipients through the header. The from * address will be used as a placeholder. */ $params = array('api_user' => $this->username, 'api_key' => $this->password, 'subject' => $mail->getSubject(), 'html' => $mail->getHtml(), 'text' => $mail->getText(), 'from' => $mail->getFrom(), 'to' => $mail->getFrom(), 'x-smtpapi' => $mail->getHeadersJson()); // determine if we should send our recipients through our headers, // and set the properties accordingly if ($mail->useHeaders()) { // workaround for posting recipients through SendGrid headers $headers = $mail->getHeaders(); $headers['to'] = $mail->getTos(); $mail->setHeaders($headers); $params['x-smtpapi'] = $mail->getHeadersJson(); } else { $params['to'] = $mail->getTos(); } if ($mail->getAttachments()) { foreach ($mail->getAttachments() as $k => $attachment) { $params['files[' . $attachment['filename'] . '.' . $attachment['extension'] . ']'] = '@' . $attachment['file']; } } return $params; }
protected function _mapToSwift(Mail $mail) { $message = new \Swift_Message($mail->getSubject()); /* * Since we're sending transactional email, we want the message to go to one person at a time, rather * than a bulk send on one message. In order to do this, we'll have to send the list of recipients through the headers * but Swift still requires a 'to' address. So we'll falsify it with the from address, as it will be * ignored anyway. */ $message->setTo($mail->getFrom()); $message->setFrom($mail->getFrom(true)); $message->setCc($mail->getCcs()); $message->setBcc($mail->getBccs()); if ($mail->getHtml()) { $message->setBody($mail->getHtml(), 'text/html'); if ($mail->getText()) { $message->addPart($mail->getText(), 'text/plain'); } } else { $message->setBody($mail->getText(), 'text/plain'); } if ($replyto = $mail->getReplyTo()) { $message->setReplyTo($replyto); } // determine whether or not we can use SMTP recipients (non header based) if ($mail->useHeaders()) { //send header based email $message->setTo($mail->getFrom()); //here we'll add the recipients list to the headers $headers = $mail->getHeaders(); $headers['to'] = $mail->getTos(); $mail->setHeaders($headers); } else { $recipients = array(); foreach ($mail->getTos() as $recipient) { if (preg_match("/(.*)<(.*)>/", $recipient, $results)) { $recipients[trim($results[2])] = trim($results[1]); } else { $recipients[] = $recipient; } } $message->setTo($recipients); } $attachments = $mail->getAttachments(); //add any attachments that were added if ($attachments) { foreach ($attachments as $attachment) { $message->attach(\Swift_Attachment::fromPath($attachment['file'])); } } //add all the headers $headers = $message->getHeaders(); $headers->addTextHeader('X-SMTPAPI', $mail->getHeadersJson()); return $message; }
/** * @see MailSender::sendMail() */ public function sendMail(Mail $mail) { $this->recipients = array(); if (count($mail->getTo()) > 0) { $this->recipients = $mail->getTo(); } if (count($mail->getCC()) > 0) { $this->recipients = array_merge($this->recipients, $mail->getCC()); } if (count($mail->getBCC()) > 0) { $this->recipients = array_merge($this->recipients, $mail->getBCC()); } // apply connection if ($this->connection === null) { $this->connect(); } // send mail $this->write('MAIL FROM:<' . $mail->getFrom() . '>'); $this->getSMTPStatus(); if ($this->statusCode != 250) { throw new SystemException($this->formatError("wrong from format '" . $mail->getFrom() . "'"), 17004); } // recipients $recipientCounter = 0; foreach ($this->recipients as $recipient) { $this->write('RCPT TO:<' . $recipient . '>'); $this->getSMTPStatus(); if ($this->statusCode != 250 && $this->statusCode != 251) { if ($this->statusCode < 550) { throw new SystemException($this->formatError("wrong recipient format '" . $recipient . "'"), 17004); } continue; } $recipientCounter++; } if (!$recipientCounter) { $this->write("RSET"); return; } // data $this->write("DATA"); $this->getSMTPStatus(); if ($this->statusCode != 354) { throw new SystemException($this->formatError("smtp error"), 17005); } $header = "Date: " . gmdate('r') . Mail::$crlf . "To: " . $mail->getToString() . Mail::$crlf . "Message-ID: <" . md5(uniqid()) . "@" . $_SERVER['SERVER_NAME'] . ">" . Mail::$crlf . "Subject: " . Mail::encodeMIMEHeader($mail->getSubject()) . Mail::$crlf . $mail->getHeader(); $this->write($header); $this->write(""); $this->write($mail->getBody()); $this->write("."); $this->getSMTPStatus(); if ($this->statusCode != 250) { throw new SystemException($this->formatError("message sending failed"), 17005); } }
/** * Prints the given mail. * * @param Mail $mail * @return string */ protected static function printMail(Mail $mail) { return "Date: " . gmdate('r') . "\n" . "To: " . $mail->getToString() . "\n" . "Subject: " . $mail->getSubject() . "\n" . $mail->getHeader() . "\n" . "Attachments: " . print_r($mail->getAttachments(), true) . "\n\n" . $mail->getMessage() . "\n\n"; }
/** * Store given mail in Database * @param \Emailing\Mail $mail * @throws \LengthException * @throws \mysqli_sql_exception */ public function save(Mail $mail) { if (empty($mail->getName())) { throw new \LengthException("Trying to save mail with empty Name"); } $mail_name = $mail->getName(); $subject = $mail->getSubject(); $textBody = $mail->getTextBody(); $htmlBody = $mail->getHtmlBody(); $to = $mail->getTo(); $cc = $mail->getCc(); $bcc = $mail->getBcc(); unset($mail); $mail_query = "insert into " . $this->prefix . "MAIL (name, subject, text_body, html_body)" . PHP_EOL . "values (?, ?, ?, ?)" . PHP_EOL . "on duplicate key update subject = values(subject), " . PHP_EOL . "text_body = values(text_body), " . PHP_EOL . "html_body = values(html_body)"; $recipient_query = "insert into " . $this->prefix . "MAIL_RECIPIENT (mail, name, email, field, status, error, date)" . PHP_EOL . "VALUES (?, ?, ?, ?, ?, ?, ?)" . PHP_EOL . "ON DUPLICATE KEY UPDATE name = values(name), " . PHP_EOL . "field = values(field), " . PHP_EOL . "status = values(status), " . PHP_EOL . "error = values(error), " . PHP_EOL . "date = values(date)"; // save mail $stm = $this->db->prepare($mail_query); if (false == $stm) { throw new \mysqli_sql_exception($this->db->errno . ': ' . $this->db->error . PHP_EOL . $this->db->error); } $stm->bind_param('ssss', $mail_name, $subject, $textBody, $htmlBody); $stm->execute(); $stm->free_result(); $stm->close(); // save recipient $stm = $this->db->prepare($recipient_query); if (false == $stm) { throw new \mysqli_sql_exception($this->db->errno . ': ' . $this->db->error . PHP_EOL . $this->db->error); } $field = 'to'; foreach ($to as $r) { $name = $r->getName(); $email = $r->getEmail(); $status = $r->getStatus(); $error = $r->getError(); $date = $r->getDate(); $stm->bind_param('sssssss', $mail_name, $name, $email, $field, $status, $error, $date); $stm->execute(); } $field = 'cc'; foreach ($cc as $r) { $name = $r->getName(); $email = $r->getEmail(); $status = $r->getStatus(); $error = $r->getError(); $date = $r->getDate(); $stm->bind_param('sssssss', $mail_name, $name, $email, $field, $status, $error, $date); $stm->execute(); } $field = 'bcc'; foreach ($bcc as $r) { $name = $r->getName(); $email = $r->getEmail(); $status = $r->getStatus(); $error = $r->getError(); $date = $r->getDate(); $stm->bind_param('sssssss', $mail_name, $name, $email, $field, $status, $error, $date); $stm->execute(); } $stm->free_result(); $stm->close(); }