PHPMailer不发送电子邮件

时间:2015-10-03 11:41:50

标签: php phpmailer

我想通过PHPMailer课程发送电子邮件。代码没有启动任何错误,但方法$mail -> Send()返回false,我不知道原因。

$ emailList看起来像:

Array(
[0] => mate80@yopmail.com
[1] => mate81@yopmail.com)

./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;">Bienvenid@!</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 invitad@ 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('no-reply@teameq.net', '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";
                }

            }
        }

错误是Send方法写成小写

2 个答案:

答案 0 :(得分:1)

此行中有错误。而不是

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

使用

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

此代码也会发送两次邮件。所以删除这一行。

$result = $mail -> Send();

答案 1 :(得分:0)

您不应该拨打Send()两次,而是检查$result

$result = $mail -> Send();

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

也;尝试使用$mail = new PHPMailer(true);代替;它应该启用例外,并告诉你是否&amp;为什么发送失败