2011-02-02 69 views
0

我需要每天发送一个基于mysql数据库中的数据的每日电子邮件。我需要它在HTML中,以便我可以把链接放在那里。我也会在电子邮件中使用图片。其基本结构为:使用mysql,html和嵌入图像的php电子邮件

这里的名单:

商家名称(这是一个链接) 简短说明 图片(这是一个链接)

商家名称(这是一个链接) 简短说明 图片(这是一个链接) 。 。 。

所有数据在MySQL数据库(虽然“图像”中的数据库是一个文件,例如/images/business/image.jpg参考)

我的问题是我应该使用PHPMailer的(我以前从来没有使用过它),或者使用这样的事情:

//set up msg 

$msg = "<html><body>Here's the list: <br /><br />"; 
while($i<numofelementsindb){ 
    $business=mysql_result($result, $i,"business"); 
    $description=mysql_result($result, $i,"description"); 
    $msg .= "The business name is <a href='www.example.com'><b>{$business}</b></a> does {$description}<br />\r\n"; 
    $i++; 
} 
$msg .= "</body> 
</html>"; 

//send 

我也不知道如何嵌入在电子邮件中的图像,因此任何建议,将不胜感激。
您是否也有任何安全建议?

谢谢

回答

0

使用我写了一个MIME电子邮件类,这里的代码做你想要的东西:

require 'class.omime.php'; 
$email = new omime('related'); 

$html = "<html><body>Here's the list: <br /><br />"; 
while($i < numofelementsindb) { 
    $business = mysql_result($result, $i,"business"); 
    $description = mysql_result($result, $i,"description"); 
    $imageFilePath = ''; # path to image file 

    $cid = $email->attachFile($imageFilePath); 
    $html.= "The business name is <a href='www.example.com'><strong>{$business}</strong></a> does {$description}<br />\r\n"; 
    $html.= "<img src=\"cid:{$cid}\" alt=\"Image of {$business}\"/><br /><br />\r\n"; 
    $i++; 
} 
$html.= "</body></html>"; 

$email->attachHTML($html); 
$email->send('[email protected]', 'Daily List of Businesses', 'from: [email protected]'); 

所有的图像将与电子邮件连接,所以你不必将它们放置在网络服务器。收件人仍然会被问到是否要显示图像。如果你有兴趣,请检查omime source page

0

我建议使用phpmailer,因为它已经准备好了几乎所有你需要的方法。尽管如此,您还可以使用简单的php mail()函数,但请注意,如果您需要一个html邮件,您需要在标题中指定MIME和内容类型,否则它将不工作(它将按原样显示标签)。

$header = 'MIME-Version: 1.0' . "\r\n"; 
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

图像可以像在网页中一样嵌入(<img src=....);请记住,默认情况下许多邮件查看器都配置为在电子邮件bodys中不加载图像,要求用户只在需要时加载它们。并且非常确定这些图像的来源,因为如果它们来自用户,它们将受到xss漏洞的影响。在这种情况下,在输出到html之前转义。

相关问题