2015-10-03 219 views
-1

我想發送和通過電子郵件PHPMailer類。該代碼不會啓動任何錯誤,但方法$mail -> Send()返回false,我不知道爲什麼。PHPMailer不發送電子郵件

$的emailList樣子:

Array(
[0] => [email protected] 
[1] => [email protected]) 

的./html/mail/index.html樣子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<!-- If you delete this meta tag, the ground will open and swallow you. --> 

<meta name="viewport" content="width=device-width" /> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>TeamEQ</title><style type="text/css">img {max-width: 100%;}body {-webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; width: 100% !important; height: 100%;}@media only screen and (max-width: 600px) { a[class="btn"] { 
display: block !important; margin-bottom: 10px !important; background-image: none !important; margin-right: 0 !important; }div[class="column"] { 
width: auto !important; float: none !important; }table.social div[class="column"] { width: auto !important;}} 

<!-- HEADER --> 

<table class="head-wrap" bgcolor="#999999" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; width: 100%; background: white; margin: 0; padding: 0;"> 

    <tr style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;"> 
     <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;"></td> 
     <td class="header container" align="" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; display: block !important; max-width: 600px !important; clear: both !important; margin: 0 auto; padding: 0;"> 

      <!-- /content --> 

      <div class="content" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; max-width: 600px; display: block; margin: 0 auto; padding: 15px;"> 
       <table style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; width: 100%; margin: 0; padding: 0;"> 
        <tr style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;"> 
         <td class="logo" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; text-align: center; margin: 0; padding: 15px;" align="center"><img src="{@urlToLogo}" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; max-width: 100%; margin: 0; padding: 0;" /></td> 
        </tr> 
       </table> 
      </div> 

      <!-- /content --> 

     </td> 
     <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;"></td> 
    </tr> 
</table> 

<!-- /HEADER --> 

<!-- BODY --> 

<table class="body-wrap" bgcolor="" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; width: 100%; margin: 0; padding: 0;"> 

    <tr style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;"> 

     <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;"></td> 

     <td class="container" align="" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; display: block !important; max-width: 600px !important; clear: both !important; margin: 0 auto; padding: 0;"> 

      <!-- content --> 

      <div class="content" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; max-width: 600px; display: block; margin: 0 auto; padding: 15px;"> 

       <table style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; width: 100%; margin: 0; padding: 0;"> 

        <tr style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;"> 

         <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;"> 

          <h1 style="font-family: 'HelveticaNeue-Light', 'Helvetica Neue Light', 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; line-height: 1.1; color: #000; font-weight: 200; font-size: 44px; margin: 0 0 15px; padding: 0;">[email protected]!</h1> 

          <p class="lead" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: normal; font-size: 17px; line-height: 1.6; margin: 0 0 10px; padding: 0;"></p> 

          <!-- A Real Hero (and a real human being) --> 

          <!--<p><img class="quote" src="https://s-media-cache-ak0.pinimg.com/236x/ad/5c/92/ad5c92c02290a4cb06dbcda734dc09da.jpg" /></p>--> 

          <p style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: normal; font-size: 14px; line-height: 1.6; margin: 0 0 10px; padding: 0;">Has sido [email protected] al equipo {@teamName} por parte de {@teamLeader}.</p> 

          <p style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: normal; font-size: 14px; line-height: 1.6; margin: 0 0 10px; padding: 0;">Para completar el registro pulsa <a href="http://app.teameq.net/#/signup/teammate/{@teamId}/{@invitationalToken}" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; color: #2BA6CB; margin: 0; padding: 0;">aquí</a>, o copia el siguiente enlace en tu navegador:</p> 

          <!-- 
           @env teameq-frontend.cloudapp.net variable de entorno. 
           Cambiar en la rama env las variables de entorno para el link de retorno del mail de invitación de usuarios. 
          --> 
          <p style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: normal; font-size: 14px; line-height: 1.6; margin: 0 0 10px; padding: 0;">http://app.teameq.net/#/signup/teammate/{@teamId}/{@invitationalToken}</p> 

          <br style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;" /> 

          <p style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: normal; font-size: 14px; line-height: 1.6; margin: 0 0 10px; padding: 0;">Gracias por tu atención, estamos ilusionados por ver cómo TeamEQ mejora tu entorno laboral!</p> 

          <!-- /hero --> 
         </td> 
        </tr> 
       </table> 
      </div> 
     </td> 
     <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;"></td> 

    </tr> 
</table> 
<!-- /BODY --> 

與下面的註釋更新的代碼是:

$teamMembers = $bbdd -> getTeam($teamId); 

     for($i=0; $i<count($emailList); $i++) 
     { 
      $userExistInTeam = false; 
      foreach($teamMembers as $key => $member) 
      { 
       if($member['email'] == $emailList[$i]) 
       { 
        $userExistInTeam = true; 
       } 
      } 

      if(!$userExistInTeam) 
      { 
       $user = insertInitialTeamMate($teamId, $emailList[$i], $teamLeader["companyId"]); 


       $emailTemplate = file_get_contents('./html/mail/index.html'); 

       $emailValues = array(

        '{@urlToLogo}' => 'http://dapalmes.com/teamEQapiRC/html/mail/img/teameq-logo.png', 
        '{@teamName}'  => $teamLeader["teamName"], 
        '{@teamLeader}'  => $teamLeader["name"] ." ". $teamLeader["surname"], 
        '{@invitationalToken}' => $user["token"], 
        '{@teamId}' => $teamId 
       ); 

       $mail = new PHPMailer(true); 
       $body = strtr($emailTemplate, $emailValues); 

       $mail -> setFrom('[email protected]', 'TeamEQ'); 

       $mail -> AddAddress($emailList[$i]); 

       $mail -> Subject = "Has sido invitado a TeamEQ"; 

       $mail -> MsgHTML($body); 

       // $result = $mail -> Send(); 

       if(!$mail->Send()) 
       { 
        echo "error al enviar!!"; 
       } 
       else 
       { 
        echo "mensaje enviado"; 
       } 

      } 
     } 

錯誤是發送方法被寫入小寫

+0

你想在這裏問一下之前想研究一下。閱讀您使用的工具的文檔有助於理解如何處理錯誤。或者在SO上尋找現有的答案。看看這裏的例子:http://stackoverflow.com/questions/2386544/error-handling-with-phpmailer也看看你的http服務器錯誤日誌文件往往有幫助。 – arkascha

+0

嗨@SubinThomas,我只是用一個emailList的例子編輯問題,並用其他評論更新代碼。謝謝! –

+0

@SubinThomas如果我刪除'='errorLog告訴我AddAdress()不是函數。 「調用未定義的方法PHPMailer :: AddAdress()」 –

回答

0

你不應該叫Send()兩次,檢查$result代替:

$result = $mail -> Send(); 

if(!$result) 
{ 
    echo "send KO"; 
} 
else 
{ 
    echo "send OK"; 
} 

也;請嘗試使用$mail = new PHPMailer(true);代替;它應該啓用例外,並會告訴你,如果&爲什麼發送失敗

+0

雖然這是一個很好的提示,但它並不能解釋爲什麼郵件沒有發送。所以這不是一個答案。 – arkascha

+0

如果作者使用'echo'輸出作爲成功的主要指示 – Sjon

+0

OP聲明郵件未發送。這完全沒有改變。 – arkascha

1

你在這一行中有錯誤。取而代之的

$mail -> AddAdress =$emailList[$i];

使用

$mail -> AddAddress($emailList[$i]); 

而且此代碼將兩次發送郵件。所以刪除這一行。

$result = $mail -> Send(); 
相關問題