Skip to content

Commit

Permalink
Merge pull request #60 from H2-invent/feature/fixSmtpServerSettings
Browse files Browse the repository at this point in the history
fix send Email problem with own server
  • Loading branch information
holema authored Feb 7, 2021
2 parents d66151c + a8b232f commit 3bfb2d2
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Service/MailerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MailerService
private $kernel;
private $logger;
private $customMailer;

private $userName;
public function __construct(LoggerInterface $logger, ParameterBagInterface $parameterBag, TransportInterface $smtp, \Swift_Mailer $swift_Mailer, KernelInterface $kernel)
{
$this->smtp = $smtp;
Expand All @@ -33,21 +33,24 @@ public function __construct(LoggerInterface $logger, ParameterBagInterface $para
$this->kernel = $kernel;
$this->logger = $logger;
$this->customMailer = null;
$this->userName = null;
}

public function buildTransport(Server $server)
{

if ($server->getSmtpHost()) {
$this->logger->debug('Build new Transport: ' . $server->getSmtpHost());
$this->logger->info('Build new Transport: ' . $server->getSmtpHost());
$tmpTransport = (new \Swift_SmtpTransport(
$server->getSmtpHost(),
$server->getSmtpPort(),
$server->getSmtpEncryption()))
->setUsername($server->getSmtpUsername())
->setPassword($server->getSmtpPassword());
$tmpMailer = new \Swift_Mailer($tmpTransport);
if ($this->customMailer !== $tmpMailer) {
if ($this->userName != $server->getSmtpUsername()) {
$this->userName = $server->getSmtpUsername();
$this->logger->info('The Transport is new and we take him');
$this->customMailer = $tmpMailer;
}
}
Expand All @@ -57,7 +60,7 @@ public function sendEmail($to, $betreff, $content, Server $server, $attachment =
{

try {
$this->logger->debug('Mail To: ' . $to);
$this->logger->info('Mail To: ' . $to);
$this->sendViaSwiftMailer($to, $betreff, $content, $server, $attachment);

} catch (\Exception $e) {
Expand All @@ -70,7 +73,7 @@ private function sendViaSwiftMailer($to, $betreff, $content, Server $server, $at
{
$this->buildTransport($server);
if ($server->getSmtpHost()) {
$this->logger->debug($server->getSmtpEmail());
$this->logger->info($server->getSmtpEmail());
$sender = $server->getSmtpEmail();
$senderName = $server->getSmtpSenderName();
} else {
Expand All @@ -93,7 +96,7 @@ private function sendViaSwiftMailer($to, $betreff, $content, Server $server, $at
if ($this->kernel->getEnvironment() === 'dev') {
$message->setTo($this->parameter->get('delivery_addresses'));
}
$this->logger->debug('Send from Custom Mailer');
$this->logger->info('Send from Custom Mailer');
$this->customMailer->send($message);
} else {
$this->swift->send($message);
Expand Down

0 comments on commit 3bfb2d2

Please sign in to comment.