到目前为止我做了什么?
我有一个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”的未定义方法。
您正在使用不存在的方法: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了解更多信息
在Command(在Symfony 3.1中检查)中,您必须使用getContainer():
$这个 - > getContainer() - >获取( '邮件') - >发送($消息);
这是一个完整的工作方法,您可以复制粘贴:
/**
* @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