diff --git a/modules/phpmailer/controllers/admin_phpmailer.php b/modules/phpmailer/controllers/admin_phpmailer.php new file mode 100644 index 00000000..9e6938df --- /dev/null +++ b/modules/phpmailer/controllers/admin_phpmailer.php @@ -0,0 +1,98 @@ +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_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_smtps = Input::instance()->post("phpmailer_smtps"); + $str_smtp_login = Input::instance()->post("phpmailer_smtp_login"); + $str_smtp_pass = Input::instance()->post("phpmailer_smtp_password"); + $str_smtp_port = Input::instance()->post("phpmailer_smtp_port"); + + // Save Settings. + 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", "smtps", $str_smtps); + module::set_var("phpmailer", "smtp_login", $str_smtp_login); + module::set_var("phpmailer", "smtp_password", $str_smtp_pass); + module::set_var("phpmailer", "smtp_port", $str_smtp_port); + 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" => "g-php-mailer-admin-form")); + + // Create the input boxes for the PHPMailer Settings + $phpmailerGroup = $form->group("PHPMailerSettings"); + $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->password("phpmailer_smtp_password") + ->label(t("SMTP Password")) + ->value(module::get_var("phpmailer", "smtp_password")); + $phpmailerSMTP->input("phpmailer_smtp_port") + ->label(t("SMTP Port")) + ->value(module::get_var("phpmailer", "smtp_port")); + $phpmailerSMTP->dropdown("phpmailer_smtps") + ->options(array("" => "", "ssl" => t("SSL"), "tls" => t("TLS"))) + ->selected(module::get_var("phpmailer", "smtps")); + + // Add a save button to the form. + $form->submit("SaveSettings")->value(t("Save")); + + // Return the newly generated form. + return $form; + } +} diff --git a/application/config/phpmailer.php b/modules/phpmailer/helpers/phpmailer_event.php similarity index 63% rename from application/config/phpmailer.php rename to modules/phpmailer/helpers/phpmailer_event.php index 64aaf95e..ac313f3a 100644 --- a/application/config/phpmailer.php +++ b/modules/phpmailer/helpers/phpmailer_event.php @@ -17,22 +17,12 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ - -/** - * to use this module: - * 1) configure your below mail settings - * 2) cd to your main gallery3 folder - * 3) be sure 'composer' for PHP is installed - * 4) run 'composer install' - * 5) enable the phpmailer module in the gallery -> admin -> module area - */ - -$config['options'] = array( - #'use_smtp' => true, - #'use_smtp_auth' => true, - #'hostname' => 'yourhostname', - #'username' => 'yourusername', - #'password' => 'yourpassword', - #'port' => '25', - #'secure' => 'tls', // or 'smtps' to enable -); +class phpmailer_event_Core { + static function admin_menu($menu, $theme) { + $menu->get("settings_menu") + ->append(Menu::factory("link") + ->id("phpmailer") + ->label(t("PHPMailer Settings")) + ->url(url::site("admin/phpmailer"))); + } +} diff --git a/modules/phpmailer/helpers/phpmailer_installer.php b/modules/phpmailer/helpers/phpmailer_installer.php new file mode 100644 index 00000000..06b5efd2 --- /dev/null +++ b/modules/phpmailer/helpers/phpmailer_installer.php @@ -0,0 +1,42 @@ +phpmail = new PHPMailer(true); - - $this->loadConfig(); - $this->headers = array(); $this->from(module::get_var("gallery", "email_from", "")); $this->reply_to(module::get_var("gallery", "email_reply_to", "")); @@ -53,54 +49,6 @@ class Sendmail_Core { $this->header_separator(empty($separator) ? "\n" : unserialize($separator)); } - protected function loadConfig() { - $this->config = Kohana::config('phpmailer'); - - if (isset($this->config['options'])) { - return true; - } - - $opts = $this->config['options']; - - if (isset($opts['use_smtp']) && $opts['use_smtp']) { - $this->phpmail->isSMTP(); - } - - if (isset($opts['use_smtp_auth']) && $opts['use_smtp_auth']) { - $this->phpmail->SMTPAuth = true; - } - - if (isset($opts['hostname'])) { - $this->phpmail->Host = $opts['hostname']; - } - - if (isset($opts['username'])) { - $this->phpmail->Username = $opts['username']; - } - - if (isset($opts['password'])) { - $this->phpmail->Password = $opts['password']; - } - - if (isset($opts['port'])) { - $this->phpmail->Port = $opts['port']; - } - - if (isset($opts['password'])) { - $this->phpmail->Host = $opts['password']; - } - - if (isset($opts['secure'])) { - $secure = strtolower($opts['secure']); - - if ($secure == 'smtps') { - $this->phpmail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; - } elseif ($secure == 'tls') { - $this->phpmail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; - } - } - } - public function __get($key) { return null; } @@ -109,11 +57,6 @@ class Sendmail_Core { switch ($key) { case "to": $this->to = is_array($value[0]) ? $value[0] : array($value[0]); - - foreach ($this->to as $to) { - $this->phpmail->addAddress($to); - } - break; case "header": if (count($value) != 2) { @@ -123,10 +66,10 @@ class Sendmail_Core { $this->headers[$value[0]] = $value[1]; break; case "from": - $this->phpmail->setFrom($value[0]); + $this->headers["From"] = $value[0]; break; case "reply_to": - $this->phpmail->addReplyTo($value[0]); + $this->headers["Reply-To"] = $value[0]; break; default: $this->$key = $value[0]; @@ -139,18 +82,73 @@ class Sendmail_Core { Kohana_Log::add("error", wordwrap("Sending mail failed:\nNo to address specified")); 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"; + } - // all modules appear to use HTML, defaulting to true - // could possibly check for a content-type header being passed in - $this->phpmail->isHTML(true); - + // 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"); - - $this->phpmail->Subject = $this->subject; - $this->phpmail->Body = $message; - - $this->phpmail->send(); - + if (!$this->mail($to, $this->subject, $message, $headers)) { + 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. + + $mail = new PHPMailer(); + + $mail->IsSMTP(); + $mail->Host = module::get_var("phpmailer", "smtp_server"); + $mail->Port = module::get_var("phpmailer", "smtp_port"); + $mail->SMTPDebug = 0; + + if (module::get_var("phpmailer", "smtp_login") != "") { + $mail->SMTPAuth = true; + + if (module::get_var("phpmailer", "smtps") == 'ssl') { + $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; + } elseif (module::get_var("phpmailer", "smtps") == 'tls') { + $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; + } + $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); + + // demdel's fix for the ecard module. + $boundaryLine = explode("\n", $message, -1); + $newboundary = substr($boundaryLine[0],2); + if (preg_match("/--/", $boundaryLine[0])) { + if (preg_match("/--".$newboundary."--/", end($boundaryLine))) { + $mail->CharSet = "UTF-8"; + $mail->ContentType = "multipart/related; boundary=\"".$newboundary."\""; + } + } + + $mail->Subject = $subject; + $mail->Body = $message; + + // Log any errors. + if (!$mail->Send()) { + Kohana_Log::add("error", wordwrap($mail->ErrorInfo)); + return false; + } else { + return true; + } + } } diff --git a/modules/phpmailer/module.info b/modules/phpmailer/module.info index 4872ce5d..6c2ce977 100644 --- a/modules/phpmailer/module.info +++ b/modules/phpmailer/module.info @@ -1,5 +1,3 @@ -name = "phpmailer" -description = "Replaces the built in mail() so you can use SMTP servers, etc. Look in application/config/phpmailer.php for installation and settings. You must do 'composer install' to make this module work." -version = 1 -author_name = "Gallery Team" -author_url = "https://galleryrevival.com/" +name = "PHPMailer" +description = "Use PHPMailer when sending email messages. Use 'composer install' to install phpmailer" +version = 3 diff --git a/modules/phpmailer/views/admin_phpmailer.html.php b/modules/phpmailer/views/admin_phpmailer.html.php new file mode 100644 index 00000000..13022bc1 --- /dev/null +++ b/modules/phpmailer/views/admin_phpmailer.html.php @@ -0,0 +1,8 @@ + +