public function book($userId, $post) { $offer = $this->getOffer($userId, $post['origin'], $post['destination'], $post['date'], $post['time']); $response = new \StdClass(); $response->success = "no"; if ($offer->success == "yes") { foreach (array('name', 'phone', 'email', 'passengerCount') as $fieldName) { ${$fieldName} = !empty($post[$fieldName]) ? $post[$fieldName] : ''; if (!${$fieldName}) { $response->errorText = "Fyll in alla felt"; print json_encode($response); exit; } } $originCity = !empty($post['originCity']) ? $post['originCity'] : ''; $destinationCity = !empty($post['destinationCity']) ? $post['destinationCity'] : ''; // save to db $booking = new Booking(); $booking->user_id = $userId; $booking->alphaId = "ZZZ" . substr("" + time(), 3, 7); $booking->origin = $offer->offerFrom; $booking->destination = $offer->offerTo; $booking->originCity = $originCity; $booking->destinationCity = $destinationCity; $booking->startUts = $offer->uts; $booking->endUts = $offer->uts + $offer->offerDurationValue; $booking->duration = $offer->offerDuration; $booking->distance = $offer->offerDistance; $booking->price = $offer->offerPrice; $booking->name = $name; $booking->email = $email; $booking->phone = $phone; $booking->passengerCount = $passengerCount; $booking->hash = md5($name . $email . $offer->offerFrom . time() . "38lx93øå"); $response->bookingSaved = $booking->save(); // mail to customer TaxiMail::sendBookingReceivedMail($booking, $userId); // mail to taxi company TaxiMail::sendNewBookingMail($booking, $userId); $response->success = "yes"; } return $response; }
/** * Send the notification. * * This method sends the notification to all recipients, rendering the template with the corresponding parameters for each recipient. * @throws phpmailerException The message could not be sent. */ public function send() { $mail = new \PHPMailer(true); $mail->CharSet = 'UTF-8'; $mail->From = $this->from_email; $mail->FromName = $this->from_name; $mail->addReplyTo($this->reply_to_email, $this->reply_to_name); $twig = static::$app->view()->getEnvironment(); // Loop through email recipients, sending customized content to each one foreach ($this->email_recipients as $recipient) { $mail->addAddress($recipient->getEmail(), $recipient->getName()); // Add any CCs and BCCs if ($recipient->getCCs()) { foreach ($recipient->getCCs() as $cc) { $mail->addCC($cc['email'], $cc['name']); } } if ($recipient->getBCCs()) { foreach ($recipient->getBCCs() as $bcc) { $mail->addBCC($bcc['email'], $bcc['name']); } } $params = $recipient->getParams(); // Must manually merge in global variables for block rendering $params = array_merge($twig->getGlobals(), $params); $mail->Subject = $this->template->renderBlock('subject', $params); $contentHtml = $this->template->renderBlock('body', $params); $mailHtml = TaxiMail::getMailHtml($params['headerLine1'], $params['headerLine2'], $contentHtml, $params['footerLine']); $mail->Body = $mailHtml; $mail->isHTML(true); // Set email format to HTML // Send mail as SMTP, if desired if (static::$app->config('mail') == 'smtp') { $config = static::$app->config('smtp'); $mail->isSMTP(true); $mail->Host = $config['host']; $mail->Port = $config['port']; $mail->SMTPAuth = $config['auth']; $mail->SMTPSecure = $config['secure']; $mail->Username = $config['user']; $mail->Password = $config['pass']; } // Try to send the mail. Will throw an exception on failure. error_log("sending..."); $mail->send(); // Clear all PHPMailer recipients (from the message for this iteration) $mail->clearAllRecipients(); } }