dsfs5420 2014-05-12 07:05 采纳率: 100%
浏览 54
已采纳

如何在zf2中使用php邮件发送电子邮件?

I use php mailer for sending email in zf2 but email not send i used gmail authentication in php mailer but when i run code it gives me following error:

SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: System error (0) SMTP connect() failed. Mailer Error: SMTP connect() failed. 

I used this Error sending e-mail using Gmail SMTP but same error exist

how i remove this error and send email? here is my code:

public function addAction()
{
    //check permissions
    if(!$this->acl->isAllowed($this->user->user_type, 'user.create'))
    $this->redirect()->toRoute('admin_index');
    //
    $this->layout()->top_heading = 'Add User';  
    $dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
    ////////////////////////////////
    $form = new UserForm();
    $update=false;
    $message='';
    if($this->getRequest()->isPost())
    {
        $post = $this->getRequest()->getPost();
        $form->setInputFilter($form->getInputFilter());
        $form->setData($post);
        //echo('<pre>');var_dump($Data);var_dump($post);echo($_POST['username']);echo($post['username']);exit;
        if($form->isValid())
        {
            $formData=$form->getData();
            $s = new User();
            $user = $dm->getRepository('Calendar\Document\User')->findOneBy(array(
                "username" => $formData['username']
            ));
            $email = $dm->getRepository('Calendar\Document\User')->findOneBy(array(
                "email" => $formData['email']
            ));
            if($user || $email)
            {
                $update=2;
                $message='User Already Exists.';    
            }
            if($post['role']=='admin')
            {
                $update=2;
                $message="Select Some Other Role."; 
            }
            else
            {
                $s->setProperty('username',$post['username']);
                $s->setProperty('password',md5($post['password']));
                $s->setProperty('email',$post['email']);
                $s->setProperty('user_type',$post['role']);
                $s->setProperty('dataentered',date('Y-m-d H:m:i'));
                $dm->persist($s);
                $dm->flush();
                //echo new Response($s->getProperty('id'));
                //
                $update=1;
                $message='User Added Successfully.';
                $form = new UserForm();
                include('module/Calendar/src/Calendar/library/class.phpmailer.php');

                $toAddress=$formData['email'];
                $mail  = new \PHPMailer();   
                $mail->IsSMTP();

                //GMAIL config
                $mail->SMTPAuth   = true;                  // enable SMTP authentication
                $mail->SMTPSecure = "ssl";                 // sets the prefix to the server
                $mail->Host ="smtp.gmail.com:465";      // sets GMAIL as the SMTP server

                $mail->Username   = "marif252@gmail.com";  // GMAIL username
                $mail->Password   = "password";            // mygmail password
                $mail->SMTPDebug = 1;
                //End Gmail

                $mail->From       = "marif252@gmail.com";
                $mail->FromName   = "Muhammad Arif";
                $mail->Subject    = "User Confirmation";
                $mail->MsgHTML("the message");

                //$mail->AddReplyTo("reply@email.com","reply name");//they answer here, optional
                $mail->AddAddress("arif.liaqat@yahoo.com","arslan");
                $mail->IsHTML(true); // send as HTML

                if(!$mail->Send()) {//to see if we return a message or a value bolean
                    echo "Mailer Error: " . $mail->ErrorInfo;
                } else  echo "Message sent!";

            }
        }
    }
    return array( 'form' => $form, 'add_message' => $message, 'update' => $update, 'user'=>$this->user );
}

how i use php mailer using gmail authentication for send email?

  • 写回答

1条回答 默认 最新

  • douliwang6896 2014-05-12 08:59
    关注

    Replace SMTP Host Part as follows :

    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465 ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办