PHPMailer error - HTML email error

467 Views Asked by At

I am new to PHP and recently I installed PHPMailer using Composer in my system. I am facing problem with an HTML message that I am trying to send.

I have tried to send plain text and it is working fine and getting the response of the PHPmailer.

Below is my code :

    <?php 
    require_once 'autoload.php';

    $mail = new PHPMailer;

    $mail->IsSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = 'password';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;

    $mail->From="[email protected]";
    $mail->FromName="My site's mailer";
    $mail->Sender="[email protected]";
    $mail->AddReplyTo("[email protected]", "Replies for my site");

    $mail->AddAddress("[email protected]");
    $mail->Subject = "Test 1";

    $mail->IsHTML(true);

    $mail->Body = '<html>
    <head>
    <link href="http://alikan.esy.es/Untitled1.css" rel="stylesheet">
    <link href="http://alikan.esy.es/index.css" rel="stylesheet">
    <script src="http://alikan.esy.es/jquery-1.11.1.min.js"></script>
    <script src="http://alikan.esy.es/fancybox/jquery.easing-1.3.pack.js"></script>
    <link rel="stylesheet" href="http://alikan.esy.es/fancybox/jquery.fancybox-1.3.0.css">
    <script src="http://alikan.esy.es/fancybox/jquery.fancybox-1.3.0.pack.js"></script>
    <script src="http://alikan.esy.es/fancybox/jquery.mousewheel-3.0.2.pack.js"></script>
    <script src="http://alikan.esy.es/wwb10.min.js"></script>
    </head>
    <body>
    <div id="wb_Text1" style="position:absolute;left:223px;top:70px;width:250px;height:16px;z-index:0;text-align:left;">
    <span style="color:#000000;font-family:Arial;font-size:13px;"><a href="javascript:displaylightbox('http://www.google.com/index.php',{width:1000,height:1000})" target="_self">This is an email body</a></span></div></body></html>';
    $mail->AltBody="This is text only alternative body.";

    if(!$mail->Send())
    {
       echo "Error sending: " . $mail->ErrorInfo;;
    }
    else
    {
       echo "Letter is sent";
    }
    ?>

And this is the error code that i get after trying to send it :

    ( ! ) Parse error: syntax error, unexpected 'http' (T_STRING) in C:\wamp\www\vendor\index.php on line 37
1

There are 1 best solutions below

8
On

You opened the body string with a single quote ':

$mail->Body = '<html>...

And you use it again in your html string to declare a parameter:

...javascript:displaylightbox('http://www.google.com/index.php',...

If your string is delimited by single or double quotes, if it contains the same character it must be escaped, otherwise the php parser will think that your string ends at the next occurrence of the opening quote. If you delimit with single quotes using double ones in the content without escaping is ok, and vice versa.

Fix it escaping those two ' this way:

...javascript:displaylightbox(\'http://www.google.com/index.php\',...

Edit:

Furthermore, if you are sending an html body, remember to set the isHTML flag before sending the mail:

$mail->IsHTML(true);