Symfony 4 Swiftmailer динамический источник - PullRequest
0 голосов
/ 19 мая 2018

Как использовать динамический source_ip при отправке электронных писем в контроллере, поскольку каждый пользователь имеет свой собственный IP-адрес для отправки электронной почты

пример:

    public function index($name, \Swift_Mailer $mailer)
{
    $message = (new \Swift_Message('Hello Email'))
        ->setFrom('send@example.com')
        ->setTo('recipient@example.com')
        ->setBody(
            $this->renderView(
                // templates/emails/registration.html.twig
                'emails/registration.html.twig',
                array('name' => $name)
            ),
            'text/html'
        )
    ;

    $mailer->send($message);

    return $this->render(...);
}

я хочу иметь возможностьдобавить ->setSourceIP(' IP address ');

Ответы [ 2 ]

0 голосов
/ 19 мая 2018

Попробуйте определить EmailService со свойством mailer следующим образом:

$transport = \Swift_SmtpTransport::newInstance($host, $port, $security)
        ->setUsername($username)
        ->setPassword($password);
$this->mailer = \Swift_Mailer::newInstance($transport);

Так что при повторной отправке почты вы можете сделать это:

$this->mailer->getTransport()->setSourceIp('8.8.8.8');
0 голосов
/ 19 мая 2018

Вы пытались создать класс, который расширяется от \Swift_Message?

/**
 * Class CustomSwiftMessage
 */
public class CustomSwiftMessage extends \Swift_Message
{
    /**
     * @var string
     */
    private $source_ip;

    /**
     * Constructor
     */
    public function __construct($subject = null, $body = null, $contentType = null, $charset = null, $source_ip = null)
    {
        parent::__construct($subject, $body, $contentType, $charset);
        $this->source_ip = $source_ip;
    }

    /**
     * Set source_ip
     * @param string $source_ip
     * @return this
     */
    public function setSourceIp($source_ip)
    {
        $this->source_ip = $source_ip;
        return $this;
    }

    /**
     * Get source_ip
     */
    public function getSourceIp()
    {
        return $this->source_ip;
    }
}
...