Last active
June 2, 2022 11:24
-
-
Save sukhikh18/0b84c831873f4df02f98d6837311e93f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Package symfony/mailer must be installed (`composer require symfony/mailer`) | |
require_once $_SERVER['DOCUMENT_ROOT'] . '/../vendor/autoload.php'; | |
use Symfony\Component\Mailer\Mailer; | |
use Symfony\Component\Mailer\Transport\SendmailTransport; | |
use Symfony\Component\Mime\Email; | |
class MailController | |
{ | |
private $mailer; | |
public function __construct() | |
{ | |
$sendmail_path = ini_get('sendmail_path') ?: null; | |
if ($sendmail_path) $sendmail_path .= " -t"; // Extract recipients from message headers. | |
$transport = new SendmailTransport($sendmail_path); | |
$this->mailer = new Mailer($transport); | |
} | |
public function actionSendCallback() | |
{ | |
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING); | |
$email = (new Email()) | |
->from(static::getFromEmail()) | |
->to('admin@localhost') | |
->subject('Request callback.') | |
->text("Name: {$_POST['name']},\n Email: {$_POST['email']}") | |
->html("Name: <strong>{$_POST['name']}</strong>,<br> Email: <strong>{$_POST['email']}</strong>"); | |
$this->mailer->send($email); | |
} | |
public static function getFromEmail() | |
{ | |
return "no-replay@{$_SERVER['SERVER_NAME']}"; | |
} | |
} | |
try { | |
$action = 'action' . ($_GET['action'] ?? ''); | |
$mailController = new MailController(); | |
if (!method_exists($mailController, $action)) throw new \Exception('Disallowed action.'); | |
$mailController->$action(); | |
} catch (\Exception $e) { | |
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment