diff --git a/composer.json b/composer.json index 3955af9..5622da4 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,8 @@ "guzzlehttp/guzzle": "^6.2", "stripe/stripe-php": "^6.24", "dompdf/dompdf": "^0.8.2", - "phpoffice/phpword": "^0.15.0" + "phpoffice/phpword": "^0.15.0", + "phpmailer/phpmailer": "^6.0" }, "license": "MPL-2.0", "authors": [ diff --git a/composer.lock b/composer.lock index a54d4e5..213b3dc 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "6f34e806ca8d5a98ea5816ce6865c253", + "content-hash": "fc511c5441078b0d99375ed15cbe16a0", "packages": [ { "name": "catfan/medoo", @@ -422,6 +422,72 @@ "homepage": "https://github.com/PhenX/php-svg-lib", "time": "2018-06-03T10:10:03+00:00" }, + { + "name": "phpmailer/phpmailer", + "version": "v6.0.6", + "source": { + "type": "git", + "url": "https://github.com/PHPMailer/PHPMailer.git", + "reference": "8190d73eb5def11a43cfb020b7f36db65330698c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/8190d73eb5def11a43cfb020b7f36db65330698c", + "reference": "8190d73eb5def11a43cfb020b7f36db65330698c", + "shasum": "" + }, + "require": { + "ext-ctype": "*", + "ext-filter": "*", + "php": ">=5.5.0" + }, + "require-dev": { + "doctrine/annotations": "1.2.*", + "friendsofphp/php-cs-fixer": "^2.2", + "phpdocumentor/phpdocumentor": "2.*", + "phpunit/phpunit": "^4.8 || ^5.7", + "zendframework/zend-eventmanager": "3.0.*", + "zendframework/zend-i18n": "2.7.3", + "zendframework/zend-serializer": "2.7.*" + }, + "suggest": { + "ext-mbstring": "Needed to send email in multibyte encoding charset", + "hayageek/oauth2-yahoo": "Needed for Yahoo XOAUTH2 authentication", + "league/oauth2-google": "Needed for Google XOAUTH2 authentication", + "psr/log": "For optional PSR-3 debug logging", + "stevenmaguire/oauth2-microsoft": "Needed for Microsoft XOAUTH2 authentication", + "symfony/polyfill-mbstring": "To support UTF-8 if the Mbstring PHP extension is not enabled (^1.2)" + }, + "type": "library", + "autoload": { + "psr-4": { + "PHPMailer\\PHPMailer\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-2.1" + ], + "authors": [ + { + "name": "Jim Jagielski", + "email": "jimjag@gmail.com" + }, + { + "name": "Marcus Bointon", + "email": "phpmailer@synchromedia.co.uk" + }, + { + "name": "Andy Prevost", + "email": "codeworxtech@users.sourceforge.net" + }, + { + "name": "Brent R. Matzelle" + } + ], + "description": "PHPMailer is a full-featured email creation and transfer class for PHP", + "time": "2018-11-16T00:41:32+00:00" + }, { "name": "phpoffice/common", "version": "0.2.9", diff --git a/lib/Email.lib.php b/lib/Email.lib.php new file mode 100644 index 0000000..acdfdcb --- /dev/null +++ b/lib/Email.lib.php @@ -0,0 +1,94 @@ +fromaddress = $address; + if (empty($name)) { + $this->fromname = $address; + } else { + $this->fromname = $name; + } + } + + public function setSubject(string $subj) { + $this->subject = $subj; + } + + public function setBody(string $body) { + $this->body = $body; + } + + public function addTo(string $email) { + $this->to[] = $email; + } + + /** + * Set the SMTP configuration. + * + * @param string $host smtp.example.com + * @param int $port 587 + * @param bool $auth true + * @param string $user + * @param string $pass + * @param string $security tls + */ + public function setSMTP(string $host = "smtp.example.com", int $port = 587, bool $auth = true, string $user = "", string $pass = "", string $security = "tls") { + $this->smtphost = $host; + $this->smtpport = $port; + $this->smtpauth = $auth; + $this->smtpusername = $user; + $this->smtppassword = $pass; + $this->smtpsecurity = $security; + } + + public function send() { + $mail = new PHPMailer(true); + $mail->isSMTP(); + $mail->Host = $this->smtphost; + $mail->SMTPAuth = $this->smtpauth; + if ($this->smtpauth) { + $mail->Username = $this->smtpusername; + $mail->Password = $this->smtppassword; + } + if ($this->smtpsecurity != "none") { + $mail->SMTPSecure = $this->smtpsecurity; + } + $mail->Port = $this->smtpport; + $mail->isHTML(false); + + $mail->setFrom($this->fromaddress, $this->fromname); + + foreach ($this->to as $to) { + $mail->addAddress($to); + } + + $mail->Subject = $this->subject; + $mail->Body = $this->body; + + $mail->send(); + } + +} diff --git a/public/actions/submitmembership.php b/public/actions/submitmembership.php index 73769aa..c1ff37f 100644 --- a/public/actions/submitmembership.php +++ b/public/actions/submitmembership.php @@ -10,6 +10,7 @@ require_once __DIR__ . "/../../lib/requiredpublic.php"; require_once __DIR__ . "/../../lib/Family.lib.php"; require_once __DIR__ . "/../../lib/Child.lib.php"; +require_once __DIR__ . "/../../lib/Email.lib.php"; function errorBack(string $errormsg) { header("Location: ../?page=signup&error=" . htmlentities($errormsg)); @@ -217,6 +218,42 @@ $database->action(function($database) { "date" => date("Y-m-d H:i:s") ]); + try { + $confirmation = new Email(); + $confirmation->addTo($family->getEmail()); + $confirmation->setFrom(SMTP_FROMADDRESS, SMTP_FROMNAME); + $confirmation->setSMTP(SMTP_HOST, SMTP_PORT, SMTP_AUTH, SMTP_USERNAME, SMTP_PASSWORD, SMTP_SECURITY); + if ($renewal) { + $confirmation->setSubject("HACHE renewal confirmation"); + $confirmation->setBody("Your membership renewal has been processed.\r\n" + . "Thanks for being a HACHE member!"); + } else { + $confirmation->setSubject("HACHE membership confirmation"); + $confirmation->setBody("Your membership and payment have been recorded.\r\n" + . "A HACHE member will be in touch in the next few days.\r\n" + . "Thanks again and welcome to HACHE!"); + } + $confirmation->send(); + } catch (Exception $e) { + + } + + try { + $notification = new Email(); + $notification->addTo(NOTIFICATION_TO); + $notification->setFrom(SMTP_FROMADDRESS, SMTP_FROMNAME); + $notification->setSMTP(SMTP_HOST, SMTP_PORT, SMTP_AUTH, SMTP_USERNAME, SMTP_PASSWORD, SMTP_SECURITY); + if ($renewal) { + $notification->setSubject("HACHE renewal notification"); + $notification->setBody("The " .$family->getName() . " family has renewed their HACHE membership."); + } else { + $notification->setSubject("HACHE membership notification"); + $notification->setBody("The " .$family->getName() . " family has registered for a HACHE membership."); + } + $notification->send(); + } catch (Exception $e) { + + } header("Location: ../?page=thanks"); return true; diff --git a/settings.template.php b/settings.template.php index b17173c..72be185 100644 --- a/settings.template.php +++ b/settings.template.php @@ -23,6 +23,16 @@ define("SITE_TITLE", "Membership Portal"); define("STRIPE_PUBKEY", ""); define("STRIPE_SECKEY", ""); +define("SMTP_HOST", ""); +define("SMTP_AUTH", true); +define("SMTP_SECURITY", "tls"); // tls, ssl, or none +define("SMTP_PORT", 25); +define("SMTP_USERNAME", ""); +define("SMTP_PASSWORD", ""); +define("SMTP_FROMADDRESS", "noreply@hachemt.org"); +define("SMTP_FROMNAME", "HACHE"); +define("NOTIFICATION_TO", ""); + // URL of the AccountHub API endpoint define("PORTAL_API", "http://localhost/accounthub/api.php"); // URL of the AccountHub home page