【问题标题】:Pear Mail Package - Embed An URL Inside E-Mail Body TextPear Mail 包 - 在电子邮件正文中嵌入 URL
【发布时间】:2012-09-04 11:07:09
【问题描述】:

我正在使用 Pear Mail 包通过 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>");

}

?>

非常感谢任何帮助,

谢谢。

【问题讨论】:

  • 您是以 HTML 电子邮件还是纯文本电子邮件发送电子邮件?
  • 我假设它是为纯文本设置的。它是 SMTP,所以我猜它不支持 HTML 标签。
  • 您可以通过 SMTP 发送 HTML 电子邮件 - 您只需在 PHP 中设置正确的设置。您要添加创建电子邮件的代码吗?
  • 谢谢 adrewsi...我在问题的编辑中添加了它。

标签: php email gmail pear


【解决方案1】:

感谢@andrewsi 的帮助。只需要花一些时间来使他的示例与 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>");

}


?>

【讨论】:

    【解决方案2】:

    看看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'    => 'you@yourdomain.com',
                  '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('postmaster@localhost', $hdrs, $body);
    
    ?> 
    

    如果您没有明确告诉它,它假定您正在发送纯文本电子邮件,因此接收客户端不会解析您的链接。

    【讨论】:

    • 感谢@andrewsi 这使我朝着正确的方向前进。只需要稍微调整一下使用 G-Mail 即可。非常感谢
    猜你喜欢
    • 2016-06-05
    • 2011-01-08
    • 2015-07-08
    • 2018-06-10
    • 2011-03-27
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 2014-09-13
    相关资源
    最近更新 更多