From 1871572255138143d2b4800ddda53efab3b99e41 Mon Sep 17 00:00:00 2001 From: eldadfux Date: Fri, 9 Aug 2019 00:49:46 +0300 Subject: [PATCH] New SMTP handler --- app/controllers/auth.php | 54 ++++++++-------- app/controllers/teams.php | 46 +++++++------- app/init.php | 30 ++++++--- app/views/console/settings/index.phtml | 7 ++- composer.json | 3 +- composer.lock | 85 +++++++++++++++++++++++--- docker-compose.yml | 24 +++++--- 7 files changed, 175 insertions(+), 74 deletions(-) diff --git a/app/controllers/auth.php b/app/controllers/auth.php index 25b1db7df6..9c94ad86a1 100644 --- a/app/controllers/auth.php +++ b/app/controllers/auth.php @@ -135,16 +135,18 @@ $utopia->post('/v1/auth/register') ->setParam('{{redirect}}', $redirect) ; - $mail = $register->get('mailgun'); /* @var $mail \MailgunLite\MailgunLite */ + $mail = $register->get('smtp'); /* @var $mail \MailgunLite\MailgunLite */ - $mail - ->addRecipient($email, $name) - ->setSubject(Locale::getText('auth.emails.confirm.title')) - ->setText(strip_tags($body->render())) - ->setHTML($body->render()) - ; + $mail->addAddress($email, $name); - if(!$mail->send()) { + $mail->Subject = Locale::getText('auth.emails.confirm.title'); + $mail->Body = $body->render(); + $mail->AltBody = strip_tags($body->render()); + + try { + $mail->send(); + } + catch(Exception $error) { if($failure) { $response->redirect($failure); return; @@ -273,16 +275,18 @@ $utopia->post('/v1/auth/register/confirm/resend') ->setParam('{{redirect}}', $redirect) ; - $mail = $register->get('mailgun'); /* @var $mail \MailgunLite\MailgunLite */ + $mail = $register->get('smtp'); /* @var $mail \MailgunLite\MailgunLite */ - $mail - ->addRecipient($user->getAttribute('email'), $user->getAttribute('name')) - ->setSubject(Locale::getText('auth.emails.confirm.title')) - ->setText(strip_tags($body->render())) - ->setHTML($body->render()) - ; + $mail->addAddress($user->getAttribute('email'), $user->getAttribute('name')); - if(!$mail->send()) { + $mail->Subject = Locale::getText('auth.emails.confirm.title'); + $mail->Body = $body->render(); + $mail->AltBody = strip_tags($body->render()); + + try { + $mail->send(); + } + catch(Exception $error) { throw new Exception('Problem sending mail: ' . $mail->getError(), 500); } @@ -503,16 +507,18 @@ $utopia->post('/v1/auth/recovery') ->setParam('{{redirect}}', $redirect) ; - $mail = $register->get('mailgun'); /* @var $mail \MailgunLite\MailgunLite */ + $mail = $register->get('smtp'); /* @var $mail \MailgunLite\MailgunLite */ - $mail - ->addRecipient($profile->getAttribute('email', ''), $profile->getAttribute('name', '')) - ->setSubject(Locale::getText('auth.emails.recovery.title')) - ->setText(strip_tags($body->render())) - ->setHTML($body->render()) - ; + $mail->addAddress($profile->getAttribute('email', ''), $profile->getAttribute('name', '')); - if(!$mail->send()) { + $mail->Subject = Locale::getText('auth.emails.recovery.title'); + $mail->Body = $body->render(); + $mail->AltBody = strip_tags($body->render()); + + try { + $mail->send(); + } + catch(Exception $error) { throw new Exception('Problem sending mail: ' . $mail->getError(), 500); } diff --git a/app/controllers/teams.php b/app/controllers/teams.php index dfe9b14ede..2516bc23cf 100644 --- a/app/controllers/teams.php +++ b/app/controllers/teams.php @@ -371,16 +371,18 @@ $utopia->post('/v1/teams/:teamId/memberships') ->setParam('{{redirect}}', $redirect) ; - $mail = $register->get('mailgun'); /* @var $mail \MailgunLite\MailgunLite */ + $mail = $register->get('smtp'); /* @var $mail \MailgunLite\MailgunLite */ - $mail - ->addRecipient($email, $name) - ->setSubject(sprintf(Locale::getText('auth.emails.invitation.title'), $team->getAttribute('name', '[TEAM-NAME]'), $project->getAttribute('name', ['[APP-NAME]']))) - ->setText(strip_tags($body->render())) - ->setHTML($body->render()) - ; + $mail->addAddress($email, $name); - if(!$mail->send()) { + $mail->Subject = sprintf(Locale::getText('auth.emails.invitation.title'), $team->getAttribute('name', '[TEAM-NAME]'), $project->getAttribute('name', ['[APP-NAME]'])); + $mail->Body = $body->render(); + $mail->AltBody = strip_tags($body->render()); + + try { + $mail->send(); + } + catch(Exception $error) { throw new Exception('Problem sending mail: ' . $mail->getError(), 500); } @@ -405,7 +407,7 @@ $utopia->post('/v1/teams/:teamId/memberships/:inviteId/resend') ->param('inviteId', '', function () {return new UID();}, 'Invite unique ID.') ->param('redirect', '', function () use ($project) {return new Host($project->getAttribute('clients', []));}, 'Reset page to redirect user back to your app from the invitation email.') ->action( - function($teamId, $inviteId, $redirect) use ($request, $response, $register, $project, $user, $audit, $projectDB) + function($teamId, $inviteId, $redirect) use ($response, $register, $project, $user, $audit, $projectDB) { $membership = $projectDB->getDocument($inviteId); @@ -419,7 +421,7 @@ $utopia->post('/v1/teams/:teamId/memberships/:inviteId/resend') throw new Exception('Team not found', 404); } - if(empty($team->getUid()) !== $teamId) { + if($team->getUid() !== $teamId) { throw new Exception('Team IDs don\'t match', 404); } @@ -450,19 +452,21 @@ $utopia->post('/v1/teams/:teamId/memberships/:inviteId/resend') ->setParam('{{redirect}}', $redirect) ; - $mail = $register->get('mailgun'); /* @var $mail \MailgunLite\MailgunLite */ + $mail = $register->get('smtp'); /* @var $mail \MailgunLite\MailgunLite */ - $mail - ->addRecipient($invitee->getAttribute('email'), $invitee->getAttribute('name')) - ->setSubject(sprintf(Locale::getText('auth.emails.invitation.title'), $team->getAttribute('name', '[TEAM-NAME]'), $project->getAttribute('name', ['[APP-NAME]']))) - ->setText(strip_tags($body->render())) - ->setHTML($body->render()) - ; + $mail->addAddress($invitee->getAttribute('email'), $invitee->getAttribute('name')); - if(!$mail->send()) { + $mail->Subject = sprintf(Locale::getText('auth.emails.invitation.title'), $team->getAttribute('name', '[TEAM-NAME]'), $project->getAttribute('name', ['[APP-NAME]'])); + $mail->Body = $body->render(); + $mail->AltBody = strip_tags($body->render()); + + try { + $mail->send(); + } + catch(Exception $error) { throw new Exception('Problem sending mail: ' . $mail->getError(), 500); } - + $audit ->setParam('userId', $user->getUid()) ->setParam('event', 'auth.invite.resend') @@ -502,7 +506,7 @@ $utopia->patch('/v1/teams/:teamId/memberships/:inviteId/status') throw new Exception('Invite not found', 404); } - if(empty($invite->getAttribute('teamId')->getUid()) !== $teamId) { + if($invite->getAttribute('teamId')->getUid() !== $teamId) { throw new Exception('Team IDs don\'t match', 404); } @@ -621,7 +625,7 @@ $utopia->delete('/v1/teams/:teamId/memberships/:inviteId') throw new Exception('Invite not found', 404); } - if(empty($invite->getAttribute('teamId')->getUid()) !== $teamId) { + if($invite->getAttribute('teamId') !== $teamId) { throw new Exception('Team IDs don\'t match', 404); } diff --git a/app/init.php b/app/init.php index 7c17b308aa..3b7d346003 100644 --- a/app/init.php +++ b/app/init.php @@ -16,6 +16,7 @@ use Database\Adapter\MySQL as MySQLAdapter; use Database\Adapter\Redis as RedisAdapter; use Utopia\Locale\Locale; use Utopia\Registry\Registry; +use PHPMailer\PHPMailer\PHPMailer; const APP_PROTOCOL = 'https'; const APP_NAME = 'Appwrite'; @@ -112,17 +113,28 @@ $register->set('cache', function() use ($redisHost, $redisPort) { // Register ca $redis->connect($redisHost, $redisPort); return $redis; }); -$register->set('mailgun', function() use ($request, $domain) { // Register MailGun handler - TODO replace with SMTP connection - $apiKey = $request->getServer('_APP_MAILGUN_KEY', ''); - $apiDomain = $request->getServer('_APP_MAILGUN_DOMAIN', ''); +$register->set('smtp', function() use ($request) { + $mail = new PHPMailer(true); - $mailgun = new \MailgunLite\MailgunLite($apiKey, $apiDomain); + $mail->isSMTP(); - $mailgun - ->setFrom('team@appwrite.io', APP_NAME . ' Team') // Notice: Error when using '.test' domain to send emails with mailgun - ; + $username = $request->getServer('_APP_SMTP_USERNAME', ''); + $password = $request->getServer('_APP_SMTP_PASSWORD', ''); - return clone $mailgun; + $mail->XMailer = 'Appwrite Mailer'; + $mail->Host = $request->getServer('_APP_SMTP_HOST', 'smtp'); + $mail->Port = $request->getServer('_APP_SMTP_PORT', 25); + $mail->SMTPAuth = (!empty($username) && !empty($password)); + $mail->Username = $username; + $mail->Password = $password; + $mail->SMTPSecure = $request->getServer('_APP_SMTP_SECURE', ''); + + $mail->setFrom('team@appwrite.io', APP_NAME . ' Team'); + $mail->addReplyTo('team@appwrite.io', APP_NAME . ' Team'); + + $mail->isHTML(true); + + return $mail; }); /** @@ -208,4 +220,4 @@ if(APP_MODE_ADMIN === $mode) { } // Set project mail -$register->get('mailgun')->setFrom(APP_EMAIL_TEAM, sprintf(Locale::getText('auth.emails.team'), $project->getAttribute('name'))); \ No newline at end of file +$register->get('smtp')->setFrom(APP_EMAIL_TEAM, sprintf(Locale::getText('auth.emails.team'), $project->getAttribute('name'))); \ No newline at end of file diff --git a/app/views/console/settings/index.phtml b/app/views/console/settings/index.phtml index 0e3b27c768..802f2aab64 100644 --- a/app/views/console/settings/index.phtml +++ b/app/views/console/settings/index.phtml @@ -167,7 +167,7 @@ use Utopia\Locale\Locale;