From 3fd249ba608795023f74958fb90d0b5eb7294585 Mon Sep 17 00:00:00 2001 From: rWatcher Date: Thu, 23 Jul 2009 07:48:13 +0800 Subject: [PATCH] Initial Commit Signed-off-by: Bharat Mediratta --- .../phpmailer/controllers/admin_phpmailer.php | 93 +++++++++++++ .../phpmailer/helpers/phpmailer_installer.php | 27 ++++ modules/phpmailer/helpers/phpmailer_menu.php | 28 ++++ modules/phpmailer/libraries/Sendmail.php | 126 ++++++++++++++++++ modules/phpmailer/module.info | 3 + .../phpmailer/views/admin_phpmailer.html.php | 5 + 6 files changed, 282 insertions(+) create mode 100644 modules/phpmailer/controllers/admin_phpmailer.php create mode 100644 modules/phpmailer/helpers/phpmailer_installer.php create mode 100644 modules/phpmailer/helpers/phpmailer_menu.php create mode 100644 modules/phpmailer/libraries/Sendmail.php create mode 100644 modules/phpmailer/module.info create mode 100644 modules/phpmailer/views/admin_phpmailer.html.php diff --git a/modules/phpmailer/controllers/admin_phpmailer.php b/modules/phpmailer/controllers/admin_phpmailer.php new file mode 100644 index 00000000..ae68a20a --- /dev/null +++ b/modules/phpmailer/controllers/admin_phpmailer.php @@ -0,0 +1,93 @@ +content = new View("admin_phpmailer.html"); + $view->content->phpmailer_form = $this->_get_admin_form(); + print $view; + } + + public function saveprefs() { + // Prevent Cross Site Request Forgery + access::verify_csrf(); + + // Figure out the values of the text boxes + $str_phpmailer_path = Input::instance()->post("phpmailer_path"); + $str_phpmailer_from_addr = Input::instance()->post("phpmailer_from_address"); + $str_phpmailer_from_name = Input::instance()->post("phpmailer_from_name"); + $str_smtp_server = Input::instance()->post("phpmailer_smtp_server"); + $str_smtp_login = Input::instance()->post("phpmailer_smtp_login"); + $str_smtp_pass = Input::instance()->post("phpmailer_smtp_password"); + + // Save Settings. + module::set_var("phpmailer", "phpmailer_path", $str_phpmailer_path); + module::set_var("phpmailer", "phpmailer_from_address", $str_phpmailer_from_addr); + module::set_var("phpmailer", "phpmailer_from_name", $str_phpmailer_from_name); + module::set_var("phpmailer", "smtp_server", $str_smtp_server); + module::set_var("phpmailer", "smtp_login", $str_smtp_login); + module::set_var("phpmailer", "smtp_password", $str_smtp_pass); + message::success(t("Your Settings Have Been Saved.")); + + // Load Admin page. + $view = new Admin_View("admin.html"); + $view->content = new View("admin_phpmailer.html"); + $view->content->phpmailer_form = $this->_get_admin_form(); + print $view; + } + + private function _get_admin_form() { + // Make a new Form. + $form = new Forge("admin/phpmailer/saveprefs", "", "post", + array("id" => "gPHPMailerAdminForm")); + + // Create the input boxes for the PHPMailer Settings + $phpmailerGroup = $form->group("PHPMailerSettings"); + $phpmailerGroup->input("phpmailer_path") + ->label(t("Location of PHPMailer Class")) + ->value(module::get_var("phpmailer", "phpmailer_path")); + $phpmailerGroup->input("phpmailer_from_address") + ->label(t("From Email Address")) + ->value(module::get_var("phpmailer", "phpmailer_from_address")); + $phpmailerGroup->input("phpmailer_from_name") + ->label(t("From Name")) + ->value(module::get_var("phpmailer", "phpmailer_from_name")); + + // Create the input boxes for the SMTP server settings + $phpmailerSMTP = $form->group("PHPMailerSMTPSettings"); + $phpmailerSMTP->input("phpmailer_smtp_server") + ->label(t("SMTP Server Address")) + ->value(module::get_var("phpmailer", "smtp_server")); + $phpmailerSMTP->input("phpmailer_smtp_login") + ->label(t("SMTP Login Name")) + ->value(module::get_var("phpmailer", "smtp_login")); + $phpmailerSMTP->input("phpmailer_smtp_password") + ->label(t("SMTP Password")) + ->value(module::get_var("phpmailer", "smtp_password")); + + // Add a save button to the form. + $form->submit("SaveSettings")->value(t("Save")); + + // Return the newly generated form. + return $form; + } +} \ No newline at end of file diff --git a/modules/phpmailer/helpers/phpmailer_installer.php b/modules/phpmailer/helpers/phpmailer_installer.php new file mode 100644 index 00000000..2c264f68 --- /dev/null +++ b/modules/phpmailer/helpers/phpmailer_installer.php @@ -0,0 +1,27 @@ +get("settings_menu") + ->append(Menu::factory("link") + ->id("phpmailer") + ->label(t("PHPMailer Settings")) + ->url(url::site("admin/phpmailer"))); + } +} diff --git a/modules/phpmailer/libraries/Sendmail.php b/modules/phpmailer/libraries/Sendmail.php new file mode 100644 index 00000000..8332b659 --- /dev/null +++ b/modules/phpmailer/libraries/Sendmail.php @@ -0,0 +1,126 @@ +headers = array(); + $config = Kohana::config("sendmail"); + foreach ($config as $key => $value) { + $this->$key($value); + } + } + + public function __get($key) { + return null; + } + + public function __call($key, $value) { + switch ($key) { + case "to": + $this->to = is_array($value[0]) ? $value[0] : array($value[0]); + break; + case "header": + if (count($value) != 2) { + throw new Exception("@todo INVALID_HEADER_PARAMETERS"); + } + $this->headers[$value[0]] = $value[1]; + break; + case "from": + $this->headers["From"] = $value[0]; + break; + case "reply_to": + $this->headers["Reply-To"] = $value[0]; + break; + default: + $this->$key = $value[0]; + } + return $this; + } + + public function send() { + if (empty($this->to)) { + throw new Exception("@todo TO_IS_REQUIRED_FOR_MAIL"); + } + $to = implode(", ", $this->to); + $headers = array(); + foreach ($this->headers as $key => $value) { + $key = ucfirst($key); + $headers[] = "$key: $value"; + } + + // The docs say headers should be separated by \r\n, but occasionaly that doesn't work and you + // need to use a single \n. This can be set in config/sendmail.php + $headers = implode($this->header_separator, $headers); + $message = wordwrap($this->message, $this->line_length, "\n"); + if (!$this->mail($to, $this->subject, $message, $headers)) { + Kohana::log("error", wordwrap("Sending mail failed:\nTo: $to\n $this->subject\n" . + "Headers: $headers\n $this->message")); + throw new Exception("@todo SEND_MAIL_FAILED"); + } + return $this; + } + + public function mail($to, $subject, $message, $headers) { + // This function is completely different from the original + // Gallery Sendmail script. Outside of this function, + // no other changes were made. + + require(module::get_var("phpmailer", "phpmailer_path")); + $mail = new PHPMailer(); + + $mail->IsSMTP(); + $mail->Host = module::get_var("phpmailer", "smtp_server"); + + if (module::get_var("phpmailer", "smtp_login") != "") { + $mail->SMTPAuth = true; + $mail->Username = module::get_var("phpmailer", "smtp_login"); + $mail->Password = module::get_var("phpmailer", "smtp_password"); + } else { + $mail->SMTPAuth = false; + } + + $mail->From = module::get_var("phpmailer", "phpmailer_from_address"); + $mail->FromName = module::get_var("phpmailer", "phpmailer_from_name"); + $mail->AddAddress($to); + $mail->IsHTML(true); + $mail->Subject = $subject; + $mail->Body = $message; + + if ($mail->Send()) { + return true; + } else { + return false; + } + } +} \ No newline at end of file diff --git a/modules/phpmailer/module.info b/modules/phpmailer/module.info new file mode 100644 index 00000000..5c5b5b98 --- /dev/null +++ b/modules/phpmailer/module.info @@ -0,0 +1,3 @@ +name = PHPMailer +description = Use PHPMailer when sending email messages. +version = 1 diff --git a/modules/phpmailer/views/admin_phpmailer.html.php b/modules/phpmailer/views/admin_phpmailer.html.php new file mode 100644 index 00000000..04a0b6f5 --- /dev/null +++ b/modules/phpmailer/views/admin_phpmailer.html.php @@ -0,0 +1,5 @@ + +
+

+ +