2015-06-14 155 views
2

我是PHP新手,最近我在我的系統中使用Composer安裝了PHPMailer。我正面臨着我試圖發送的HTML郵件的問題。PHPMailer錯誤 - HTML電子郵件錯誤

我試圖發送純文本,它工作正常,並獲得PHPmailer的響應。

下面是我的代碼:

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

這是錯誤代碼,我試圖把它後獲得:

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

回答

0

你用單引號'打開身體的字符串:

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

然後在html字符串中再次使用它來聲明一個參數:

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

如果字符串是單或雙引號分隔,如果它包含相同的字符必須逃脫,否則PHP解析器會認爲你的字符串在開引號的下一次出現結束。如果您使用單引號分隔內容中的雙引號而不轉義,則反之亦然。

修復它逃脫這兩個'這樣:

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

編輯:

此外,如果您要發送的HTML身體,記得在發送郵件之前設置isHTML標誌:

$mail->IsHTML(true); 
+0

非常感謝您的回答我真的很感激它,我試過了您的建議,併發送了emai,但收到的消息只是一個不可單擊的文本(純文本)... –

+0

您需要添加'$ mail-> IsHTML(true);'。 –

+0

再次感謝你,但如果你檢查代碼源,你會發現我已經包含了$ mail-> IsHTML(true);並讓你知道我已經在$ mail-> Body之前嘗試過兩種方式,之後它並沒有工作... –