【问题标题】:How to test sending an email in symfony 4 and Swiftmailer如何在 symfony 4 和 Swiftmailer 中测试发送电子邮件
【发布时间】:2019-04-09 13:59:48
【问题描述】:

这可能看起来很愚蠢,但我想知道如何测试从 Gmail 发送电子邮件的功能,而不是从命令行发送电子邮件,因为它工作正常。我想如何调用发送电子邮件的函数。我关注了这个tutorial。 我的 config/packages/imap.yaml

imap:
    connections:
        example_connection:
            mailbox: "{imap.gmail.com:993/imap/ssl}INBOX"
            username: "kadrad26666@gmail.com"
            password: "password"

        another_connection:
            mailbox: "{localhost:143}INBOX"
            username: "username"
            password: "password"
            attachments_dir: "%kernel.root_dir%/../var/imap/attachments"
            server_encoding: "UTF-8"

IndexController中我的Index函数

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

    $mailer->send($message);

    return $this->render('registration.html.twig'); }

我的树枝如下

 <div class="box box-solid alert-block">
  <div class="box-header">
    <h3 class="box-title"></h3>
  </div>
  <div class="box-body">
   {{render( controller('App\\Controller\\IndexController::index("hello")') ) }} 
  </div>
</div>

我卡在index($name, \Swift_Mailer $mailer)中,应该给什么参数。

【问题讨论】:

    标签: php symfony sonata-admin swiftmailer symfony4


    【解决方案1】:

    我认为这不是最有效的方法。包括模板片段在某些情况下有效,但我怀疑这是否是此特定场景的正确方法。

    但是,如果你真的想这样做,你可以创建一个Swift_Mailer 对象并作为参数传递给twig

    // Create the Mailer using your created Transport
    $mailer = new Swift_Mailer($transport);
    
    // Create a message
    $message = (new Swift_Message('Wonderful Subject'))
      ->setFrom(['john@doe.com' => 'John Doe'])
      ->setTo(['receiver@domain.org', 'other@domain.org' => 'A name'])
      ->setBody('Here is the message itself')
      ;
    
    // Pass the $mailer to the twig as a parameter.
    

    更多:- https://swiftmailer.symfony.com/docs/messages.html

    我要做的是创建一个控制器函数,它调用一个服务函数来发送电子邮件并将该控制器调用嵌入到树枝(就像你已经在做的那样)。在该控制器功能中,您不想接受参数。让自动连接处理服务所需的参数。

    干杯。

    【讨论】:

    • 感谢您的帮助,但我仍然不明白 $mailer = new Swift_Mailer($transport); 行应该放在哪里。它应该进入public function index($name, \Swift_Mailer $mailer) { 函数
    • 它应该在你调用twig文件的函数内部。
    • 我确实这样做了,我将邮件变量添加到 twig 使用return $this-&gt;renderView('Block/block_alert.html.twig' , array('mailer' =&gt; $mailer)); 并调用如下{{render( controller('App\\Controller\\IndexController::index("hello" , ' ~ mailer ~ ' )') ) }} ,我知道,这应该是另一个问题但我收到一条错误消息 Variable "mailer" does not exist. 即使我在数组中使用了一个字符串,这意味着该变量没有传递给 twig 模板
    • 不应该是这样的吗? {{render( controller('App\\Controller\\IndexController::index("hello" , mailer)') ) }} 。没有引号和东西?
    • 我尝试连接变量,但根据您的建议,我收到错误消息 在呈现模板期间引发了异常(“URI 的控制器”/_fragment 不是可调用。“App\Controller\IndexController”类的预期方法“index("hello", mailer)”。可用方法:“indexAction”、“index”、“setContainer”、“getSubscribedServices”。”)。
    猜你喜欢
    • 1970-01-01
    • 2015-04-16
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    相关资源
    最近更新 更多