如何使用swift邮件程序库在symfony2.7中发送电子邮件?

问题描述 投票:-2回答:3

到目前为止我做了什么?

我有一个webController类这个类我创建pingserviceAction我想发送电子邮件到所有网址,即在我的情况下primary_url我怎么能实现这一点谢谢你提前

控制器的源代码如下

 <?php
  namespace MWANMOBILE\Bundle\BIBundle\Controller\Admin;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\HttpFoundation\Response;
  use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;

  use MWANMOBILE\Bundle\BIBundle\Entity\Web;
  use MWANMOBILE\Bundle\BIBundle\Form\Type\ServiceType;
  use MWANMOBILE\Bundle\BIBundle\Form\Type\UserType;

  class WebController extends Controller
   {
      public function pingserviceAction(Request $request)
        {
        $em = $this->getDoctrine()->getManager();

        $web_list = $em->getRepository('MWANMOBILEBIBundle:Web')->allWeb();

    //  $web_url = $em->getRepository('MWANMOBILEBIBundle:Web')->allWebUrls();
    // var_dump($web_list); 
    //  exit();

        $site_status = '';

        foreach ($web_list as $single_web_list)
        {

        $url= $single_web_list['primary_url'];
        $st_email = $single_web_list['status_email'];
        $st_message = $single_web_list['status_message'];


        $ch = curl_init($url);

        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_exec($ch);
        $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if (!200==$retcode) {

            echo("comon"); 
            $subject= "sorry server is down due to maintenance work ";

            $site_status.='site_down ';

            $to = array('[email protected]');
            $this->getMailer()->composeAndSend('[email protected]', $to, $subject , $st_message);

        } else
         {
            $site_status.='site_active ';
         }

        }

        exit();
 }

}

尝试调用类“MWANMOBILE \ Bundle \ BIBundle \ Controller \ Admin \ WebController”的名为“getMailer”的未定义方法。

symfony email swiftmailer
3个回答
2
投票

您正在使用不存在的方法:getMailer(在Symfony 1.x中有这样的方法)

要获得邮件,您需要通过调用$this->get('mailer')获取邮件服务并使用send方法,该方法将Swift_Message实例作为参数。所以你需要做的就是替换:

$this->getMailer()->composeAndSend('[email protected]', $to, $subject , $st_message);

有:

 $message = \Swift_Message::newInstance()
    ->setSubject($subject)
    ->setFrom('[email protected]')
    ->setTo($to)
    ->setBody($st_message);

 $this->get('mailer')->send($message);

查看official howto了解更多信息


0
投票

在Command(在Symfony 3.1中检查)中,您必须使用getContainer():

$这个 - > getContainer() - >获取( '邮件') - >发送($消息);


0
投票

这是一个完整的工作方法,您可以复制粘贴:

/**
 * @param $to
 * @param $from
 * @param $subject
 * @param $body
*/
protected function sendEmail($to, $from, $subject, $body)
{
    $email = \Swift_Message::newInstance()
        ->setSubject($subject)
        ->setFrom($from)
        ->setTo($to)
        ->setContentType("text/html")
        ->setBody($body,'text/html');

        $this->container->get('mailer')->send($email);
}

它也在这里说:https://reformatcode.com

© www.soinside.com 2019 - 2024. All rights reserved.