2012-09-11 32 views
0

我正在使用梨形郵件包通過G-Mail(使用SMTP)爲我正在開發的網站發送電子郵件。當有人在網站上註冊時,用戶必須先在他們收到的確認電子郵件中點擊鏈接,然後才能創建其帳戶。我遇到的問題是,我在電子郵件中嵌入的URL字面上顯示爲我鍵入它(用HTML標記顯示),即梨形郵件包 - 在電子郵件正文中嵌入網址

<a href = '...'>...</a> 

,而不是一個可點擊的URL。有關如何讓網址可點擊的建議?我的代碼是或多或少一樣的以下問題:Send email using the GMail SMTP server from a PHP page

<?php 

require_once "/Mail/Mail-1.2.0/Mail.php"; 
require_once "/Mail/Mail-1.2.0/Mail_Mime-1.8.5/Mail/mime.php"; 

$from = "XYZ <XYZ.gmail.com>"; 
$email = $_GET['e']; 
$to = $email; 
$subject = "XYZ - Registration"; 
$body = "<html><body>Hi " . $_GET['f'] . ",\n\nThank You For Registering With XYZ\n\nPlease Click The Following Link To Confirm Your Account: <a href = 'www.test.com'>www.test.com</a></body></html>"; 
$crlf = "\n"; 

$mime = new Mail_mime($crlf); 
$mime->setHTMLBody($body); 
$body = $mime->get(); 

$host = "ssl://smtp.gmail.com"; 
$port = "465"; 
$username = "XYZ"; 
$password = "XYZPassword"; 

$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject); 
$smtp = Mail::factory('smtp', array('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password) ); 

$mail = $smtp->send($to, $headers, $body); 

if (PEAR::isError($mail)) 
{ 

    echo("<p>" . $mail->getMessage() . "</p>"); 

} 

else 
{ 

    echo("<p>Message successfully sent!</p>"); 

} 

?> 

任何幫助是非常讚賞,

感謝。

+0

您是將您的電子郵件作爲HTML電子郵件發送還是以純文本形式發送? – andrewsi

+0

我假設它設置爲純文本。這是SMTP,所以我猜想它不支持HTML標籤。 – slickboy

+0

你可以通過SMTP發送HTML郵件 - 你只需要在你的PHP中設置正確的設置。你想添加創建你的電子郵件的代碼嗎? – andrewsi

回答

0

看一看的PEAR manual page

<?php 

include 'Mail.php'; 
include 'Mail/mime.php' ; 

$text = 'Text version of email'; 
$html = '<html><body>HTML version of email</body></html>'; 
$file = '/home/richard/example.php'; 
$crlf = "\n"; 
$hdrs = array(
       'From' => '[email protected]', 
       'Subject' => 'Test mime message' 
      ); 

$mime = new Mail_mime(array('eol' => $crlf)); 

$mime->setTXTBody($text); 
$mime->setHTMLBody($html); 
$mime->addAttachment($file, 'text/plain'); 

$body = $mime->get(); 
$hdrs = $mime->headers($hdrs); 

$mail =& Mail::factory('mail'); 
$mail->send('[email protected]', $hdrs, $body); 

?> 

如果你沒有明確告訴它,它假設你發送一個純文本電子郵件,並讓你的鏈接不會被解析接收客戶。

+0

謝謝@andrewsi這導致我在正確的方向。只需要小心它與G-Mail一起工作。非常感謝 – slickboy

+0

不客氣 - 很高興有所幫助。 – andrewsi

0

感謝@andrewsi爲他的幫助。只需製作一些小小的Tweeks使他的例子與G-Mail一起工作。

<?php 

require_once "/Mail/Mail-1.2.0/Mail.php"; 
require_once "/Mail/Mail-1.2.0/Mail_Mime-1.8.5/Mail/mime.php"; 

$from = "XYZ <XYZ.gmail.com>"; 
$email = $_GET['e']; 
$to = $email; 
$subject = "xyz - Registration"; 
$body = "<html><body>Hi " . $_GET['f'] . ",\n\nThank You For Registering With xyz\n\nPlease Click The Following Link To Confirm Your Account: <a href = 'www.test.com'>www.test.com</a></body></html>"; 

$host = "ssl://smtp.gmail.com"; 
$port = "465"; 
$username = "XYZ"; 
$password = "XYZPassword"; 

$crlf = "\n"; 
$hdrs = array(
       'From' => $from, 
       'Subject' => $subject 
      ); 

$mime = new Mail_mime(array('eol' => $crlf)); 

$mime->setHTMLBody($body); 

$body = $mime->get(); 
$hdrs = $mime->headers($hdrs); 
$mail =& Mail::factory('smtp', array('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password) ); 
$mail->send($to, $hdrs, $body); 

if (PEAR::isError($mail)) 
{ 

    echo("<p>" . $mail->getMessage() . "</p>"); 

} 

else 
{ 

    echo("<p>Message successfully sent!</p>"); 

} 


?> 
相關問題