簡體   English   中英

如何在zf2中使用php mailer發送電子郵件?

[英]How to send email using php mailer in zf2?

我使用php mailer在zf2中發送電子郵件,但電子郵件未發送,我在php mailer中使用了gmail身份驗證,但是當我運行代碼時,它給了我以下錯誤:

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

我使用此錯誤通過Gmail SMTP發送電子郵件,但存在相同的錯誤

我如何刪除此錯誤並發送電子郵件? 這是我的代碼:

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 );
}

我如何使用使用Gmail身份驗證的php mailer發送電子郵件?

替換SMTP主機部分,如下所示:

$mail->Host = "smtp.gmail.com";
$mail->Port = 465 ;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM