symfony-mailer-example/mailer.php

25 lines
864 B
PHP
Raw Permalink Normal View History

2022-12-04 05:41:35 +00:00
<?php
require_once 'vendor/autoload.php';
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Email;
use Dotenv\Dotenv;
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
$dotenv->required(['MAILER_PREFIX','MAILER_PWD','MAILER_SUFFIX']);
$dotenv->required('MAILER_PREFIX')->notEmpty();
$dotenv->required('MAILER_PWD')->notEmpty();
$dotenv->required('MAILER_SUFFIX')->notEmpty();
$dsn = $_ENV['MAILER_PREFIX'] . urlencode($_ENV['MAILER_PWD']) . $_ENV['MAILER_SUFFIX'];
$transport = Transport::fromDsn($dsn);
$mailer = new Mailer($transport);
$email = (new Email())
->from('Tester' . '<' . 'tester@example.com' .'>')
->to('hello@adamsdesk.com')
->subject('Contact Form Submission')
->text('Hello world, this is a test of the php email sender.');
$sendResult = $mailer->send($email);
?>