2015-10-03 172 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(); 
相关问题