From 28ed02091a3bac063bc1f1fc243694d78018632a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 14 Jun 2020 17:14:41 +0200 Subject: [PATCH] MAPG-69 add mailing --- .env.example | 3 ++ composer.json | 3 +- composer.lock | 70 +++++++++++++++++++++++++++++++++++++- docker-compose.yml | 6 ++++ src/Mailing/Mail.php | 81 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 src/Mailing/Mail.php diff --git a/.env.example b/.env.example index cff20cf..cdc2cd4 100644 --- a/.env.example +++ b/.env.example @@ -8,3 +8,6 @@ GOOGLE_MAPS_JS_API_KEY=your_google_maps_js_api_key LEAFLET_TILESERVER_URL=a_leaflet_compatible_tileserver_url LEAFLET_TILESERVER_ATTRIBUTION=attribution_to_be_shown_for_tiles STATIC_ROOT=/static +MAIL_FROM=mapguesser@mapguesser-dev.ch +MAIL_HOST=mail +MAIL_PORT=2500 diff --git a/composer.json b/composer.json index 0b434e7..4b32718 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,8 @@ "license": "GNU GPL 3.0", "require": { "vlucas/phpdotenv": "^4.1", - "symfony/console": "^5.1" + "symfony/console": "^5.1", + "phpmailer/phpmailer": "^6.1" }, "require-dev": {}, "autoload": { diff --git a/composer.lock b/composer.lock index ad2a23a..65cf8b5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,76 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "13a0eaff2786786caff2be86ac704fc7", + "content-hash": "67a75c3149ef859545476427e7f2f686", "packages": [ + { + "name": "phpmailer/phpmailer", + "version": "v6.1.6", + "source": { + "type": "git", + "url": "https://github.com/PHPMailer/PHPMailer.git", + "reference": "c2796cb1cb99d7717290b48c4e6f32cb6c60b7b3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/c2796cb1cb99d7717290b48c4e6f32cb6c60b7b3", + "reference": "c2796cb1cb99d7717290b48c4e6f32cb6c60b7b3", + "shasum": "" + }, + "require": { + "ext-ctype": "*", + "ext-filter": "*", + "php": ">=5.5.0" + }, + "require-dev": { + "doctrine/annotations": "^1.2", + "friendsofphp/php-cs-fixer": "^2.2", + "phpunit/phpunit": "^4.8 || ^5.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-only" + ], + "authors": [ + { + "name": "Marcus Bointon", + "email": "phpmailer@synchromedia.co.uk" + }, + { + "name": "Jim Jagielski", + "email": "jimjag@gmail.com" + }, + { + "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", + "funding": [ + { + "url": "https://github.com/synchro", + "type": "github" + } + ], + "time": "2020-05-27T12:24:03+00:00" + }, { "name": "phpoption/phpoption", "version": "1.7.3", diff --git a/docker-compose.yml b/docker-compose.yml index 3effcfe..b506eb1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,6 +8,7 @@ services: - .:/var/www/mapguesser links: - 'mariadb' + - 'mail' mariadb: image: mariadb:10.1 volumes: @@ -17,5 +18,10 @@ services: MYSQL_DATABASE: 'mapguesser' MYSQL_USER: 'mapguesser' MYSQL_PASSWORD: 'mapguesser' + mail: + image: marcopas/docker-mailslurper:latest + ports: + - 8080:8080 + - 8085:8085 volumes: mysql: diff --git a/src/Mailing/Mail.php b/src/Mailing/Mail.php new file mode 100644 index 0000000..4c75dcb --- /dev/null +++ b/src/Mailing/Mail.php @@ -0,0 +1,81 @@ +recipients[] = [$mail, $name]; + } + + public function setSubject(string $subject): void + { + $this->subject = $subject; + } + + public function setBody(string $body): void + { + $this->body = $body; + } + + public function setBodyFromTemplate(string $template, array $params = []): void + { + $this->body = file_get_contents(ROOT . '/mail/' . $template . '.tpl'); + + foreach ($params as $key => $param) { + $this->body = str_replace('{{' . $key . '}}', $param, $this->body); + } + } + + public function send(): void + { + $mailer = new PHPMailer(true); + + $mailer->CharSet = 'utf-8'; + $mailer->Hostname = substr($_ENV['MAIL_FROM'], strpos($_ENV['MAIL_FROM'], '@') + 1); + + if (!empty($_ENV['MAIL_HOST'])) { + $mailer->Mailer = 'smtp'; + $mailer->Host = $_ENV['MAIL_HOST']; + $mailer->Port = !empty($_ENV['MAIL_PORT']) ? $_ENV['MAIL_PORT'] : 25; + $mailer->SMTPSecure = !empty($_ENV['MAIL_SECURE']) ? $_ENV['MAIL_SECURE'] : ''; + + if (!empty($_ENV['MAIL_USER'])) { + $mailer->SMTPAuth = true; + $mailer->Username = $_ENV['MAIL_USER']; + $mailer->Password = $_ENV['MAIL_PASSWORD']; + } else { + $mailer->SMTPAuth = false; + } + } else { + $mailer->Mailer = 'mail'; + } + + $mailer->setFrom($_ENV['MAIL_FROM'], 'MapGuesser'); + $mailer->addReplyTo($_ENV['MAIL_FROM'], 'MapGuesser'); + + $mailer->Sender = !empty($_ENV['MAIL_BOUNCE']) ? $_ENV['MAIL_BOUNCE'] : $_ENV['MAIL_FROM']; + + $mailer->Subject = $this->subject; + $mailer->Body = $this->body; + + foreach ($this->recipients as $recipient) { + $this->sendMail($mailer, $recipient); + } + } + + private function sendMail(PHPMailer $mailer, array $recipient) + { + $mailer->clearAddresses(); + $mailer->addAddress($recipient[0], $recipient[1]); + + $mailer->send(); + } +}