swiftmailer symfony code example
Example: send email with swiftmailer symfony
<?php
namespace App\CostomClass;
use App\Repository\ParametreRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;
Class SendMail{
function __construct( $Email , $Subject , $text )
{
$this->Email = $Email ;
$this->Subject = $Subject ;
$this->text = $text ;
}
public function sendMessageWithSMTP()
{
$transport = (new \Swift_SmtpTransport('smtp.office365.com', 25, 'tls'))
->setUsername('[email protected]')
->setPassword('YourPassword');
$transport->setLocalDomain('[127.0.0.1]');
$mailer = new \Swift_Mailer($transport);
try {
$message = (new \Swift_Message($this->Subject))
->setFrom(array('[email protected]' => "Subject"))
->setTo( $this->Email)
->setBody($this->text, 'text/html');
$mailer->send($message);
} catch (\Exception $e) {
return $e->getMessage();
}
return 1;
}
public function sendMessageWithGmail()
{
$transport = (new \Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl'))
->setUsername('[email protected]')
->setPassword('Password');
$transport->setLocalDomain('[127.0.0.1]');
$mailer = new \Swift_Mailer($transport);
try {
$message = (new \Swift_Message($this->Subject))
->setFrom(array('[email protected]' => "Subject"))
->setTo( $this->Email)
->setBody($this->text, 'text/html');
$mailer->send($message);
} catch (\Exception $e) {
return $e->getMessage();
}
return 1;
}
}