-1

I would like to send and email trough the PHPMailer class. The code doesn't launch any error but the method $mail -> Send() returns false and I don't know why.

$emailList looks like:

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

The ./html/mail/index.html looks like:

<!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 -->

The code updated with comments below is:

$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";
                }

            }
        }

the error was that the Send method is written lower case

Jongware
  • 22,200
  • 8
  • 54
  • 100
  • You want to research a bit before asking here. Reading the documentation of the tools you use help understanding how to handle errors. Or look for existing answers here on SO. Take a look here for example: http://stackoverflow.com/questions/2386544/error-handling-with-phpmailer Also taking a look into your http servers error log file often helps. – arkascha Oct 03 '15 at 11:48
  • Hi @SubinThomas, I just edit the question with an example of emailList and code updated with other comments. Thanks! – Icar Gongora Oct 03 '15 at 12:40
  • @SubinThomas if I remove the '=' the errorLog says me that AddAdress() is not a function . "Call to undefined method PHPMailer::AddAdress()" – Icar Gongora Oct 03 '15 at 12:48
  • @IcarGongora : Spelling error use *AddAddress()*. – Subin Thomas Oct 03 '15 at 12:52
  • I just updated the question with the mail code @SubinThomas – Icar Gongora Oct 03 '15 at 12:55
  • I just corrected and the error now is: ini_set() has been disabled for security reasons @SubinThomas. thanks! – Icar Gongora Oct 03 '15 at 12:59
  • http://superuser.com/questions/833077/how-to-remove-error-warning-ini-set-has-been-disabled-for-security-reasons this will help you in *ini_set()*. But mail is sending?? @IcarGongora – Subin Thomas Oct 03 '15 at 13:02
  • Thanks! I'm gonna check it. email is not sending because phpmailer is launching the ErrorException @SubinThomas – Icar Gongora Oct 03 '15 at 13:04
  • echo $emailList[$i]; and confirm it is a string. ***** remove *true* from *$mail = new PHPMailer(true);* and change it to *$mail = new PHPMailer();* @IcarGongora – Subin Thomas Oct 03 '15 at 13:13
  • Why not look at the mailing list example provided with PHPMailer that is similar to what you're doing, but much more efficient and lacks the bugs? – Synchro Oct 03 '15 at 15:10

2 Answers2

1

You have an error in this line . Instead of

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

Use

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

Also this code will send the mail twice. so remove this line.

$result = $mail -> Send();
Subin Thomas
  • 1,408
  • 10
  • 19
0

You shouldn't be calling Send() twice, check for $result instead:

$result = $mail -> Send();

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

Also; try using $mail = new PHPMailer(true); instead; it should enable Exceptions and will tell you if & why sending failed

Sjon
  • 4,989
  • 6
  • 28
  • 46