我如何使用 swift 邮件程序库在 symfony2.7 中发送电子邮件消息?
How do i use swift mailer library to send email message in symfony2.7?
到目前为止我做了什么?
我有一个 webController class 这个 class 我创建了 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('zarghamirtza@gmail.com');
$this->getMailer()->composeAndSend('blueeyed_riu@yahoo.com', $to, $subject , $st_message);
} else
{
$site_status.='site_active ';
}
}
exit();
}
}
试图调用 class "MWANMOBILE\Bundle\BIBundle\Controller\Admin\WebController" 的名为 "getMailer" 的未定义方法。
您使用的方法不存在:getMailer
(Symfony 1.x
中有这样的方法)
要获取邮件程序,您需要通过调用 $this->get('mailer')
并使用 send
方法获取邮件程序服务,该方法将 Swift_Message
实例作为参数。所以你需要做的就是替换:
$this->getMailer()->composeAndSend('blueeyed_riu@yahoo.com', $to, $subject , $st_message);
与:
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom('blueeyed_riu@yahoo.com')
->setTo($to)
->setBody($st_message);
$this->get('mailer')->send($message);
查看 official howto 了解更多信息
这里有一个完整的工作方法,你可以复制粘贴:
/**
* @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
在命令中(在 Symfony 3.1 中检查)你必须使用 getContainer():
$this->getContainer()->get('mailer')->send($message);
到目前为止我做了什么?
我有一个 webController class 这个 class 我创建了 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('zarghamirtza@gmail.com');
$this->getMailer()->composeAndSend('blueeyed_riu@yahoo.com', $to, $subject , $st_message);
} else
{
$site_status.='site_active ';
}
}
exit();
}
}
试图调用 class "MWANMOBILE\Bundle\BIBundle\Controller\Admin\WebController" 的名为 "getMailer" 的未定义方法。
您使用的方法不存在:getMailer
(Symfony 1.x
中有这样的方法)
要获取邮件程序,您需要通过调用 $this->get('mailer')
并使用 send
方法获取邮件程序服务,该方法将 Swift_Message
实例作为参数。所以你需要做的就是替换:
$this->getMailer()->composeAndSend('blueeyed_riu@yahoo.com', $to, $subject , $st_message);
与:
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom('blueeyed_riu@yahoo.com')
->setTo($to)
->setBody($st_message);
$this->get('mailer')->send($message);
查看 official howto 了解更多信息
这里有一个完整的工作方法,你可以复制粘贴:
/**
* @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
在命令中(在 Symfony 3.1 中检查)你必须使用 getContainer():
$this->getContainer()->get('mailer')->send($message);