使用内嵌图像在php中发送电子邮件

问题描述:

我使用php的内置邮件功能发送电子邮件。到目前为止,我通过查看其他示例并剥离了我认为必要的东西,从而取得了很多成就。使用内嵌图像在php中发送电子邮件

我目前使用内嵌图像发送电子邮件的代码如下所示。该内容是通过使用其可以或可以不具有由值替换从我的数据库(在此省略)

function sendEmail($to, $subject, $htmlmessage, $inline_images = array()){ 
    define('RN', "\n"); 
    /* define mime boundaries */ 
    $mime_boundary_mix = "_mix_" . md5(time()); 

    /* set headers */ 
    $headers = "From: [email protected]" . RN; 
    $headers .= "Reply-To: [email protected]" . RN; 
    $headers .= "Return-Path: [email protected]" . RN; 
    $headers .= "Message-ID: <".time()."[email protected]".$_SERVER['SERVER_NAME'].">" . RN; 
    $headers .= "X-Mailer: PHP v".phpversion() . RN; 

    $headers .= "Mime-Version: 1.0" . RN; 
    $headers .= "Content-Type: multipart/mixed;" . RN; 
    $headers .= " boundary=\"{$mime_boundary_mix}\""; 

    /* start email body with mime_boundary */ 
    $msg = "--{$mime_boundary_mix}" . RN; 
    /* include html */ 
    $msg .= "Content-Type: text/html; charset=\"iso-8859-15\"". RN; 
    $msg .= "Content-Transfer-Encoding: 8bit". RN . RN; 
    $msg .= $htmlmessage . RN; 

    /* check if there are inline images */ 
    if(count($inline_images) > 0) { 
     foreach($inline_images as $image) { 
     $path_to_image = $image['path']; 
     $image_filename = basename($path_to_image); 
     $image_type = filetype($path_to_image); 

     $file_handler = fopen($path_to_image, 'rb'); 
     $image_data = chunk_split(base64_encode(fread($file_handler, filesize($path_to_image)))); 
     fclose($file_handler); 

     /* start mime boundary */ 
     $msg .= "--{$mime_boundary_mix}" . RN; 
     /* add image data */ 
     $msg .= "Content-Type: {" . image_type_to_mime_type (exif_imagetype($path_to_image)) . "};" . RN; 
     $msg .= " name=\"{$image_filename}\"" . RN; 
     $msg .= "Content-Disposition: inline;" . RN; 
     $msg .= " filename=\"{$image_filename}\"" . RN; 
     $msg .= "Content-Transfer-Encoding: base64" . RN . RN; 
     $msg .= $image_data . RN . RN; 

     } 
     /* end mime boundary */ 
     $msg .= "--{$mime_boundary_mix}--" . RN; 

    } 
    /* end mime boundary */ 
    $msg .= "--{$mime_boundary_mix}--" . RN; 

    return mail($to, $subject, $msg, $headers); 
} 

$ inline_images一些占位符的模板生成是其中包含与路径项像图像的数组: 阵列([0] =>阵列(“路径” =>“myimage.png”))(如果我没有记错的话,但是这并不重要)

谁能告诉我,为什么使用此代码,展望将附件图标放在这封电子邮件中,为什么Gmail会显示警告:您是否想要显示所有图像,并且在允许这些图像时不显示任何图像? 关于此问题的一些问题: - 在Outlook中接收此电子邮件可以正常工作,但如果可能,我想隐藏回形针图标,因为它是嵌入式图像,并非真正的附件。 - 在GMail中,我收到电子邮件中所有图像位置的图像作为附件和空白(破碎的图标 - 图标),我该如何解决?如果我想允许图片,GMail会首先问我,但在允许空白出现之后。我还可以将图像作为附件隐藏在Gmail中吗? - 我也尝试在Office Outlook Web Access中查看电子邮件,该电子邮件以与GMail相同的方式显示电子邮件:图片未显示,但仅作为附件显示。

任何人都可以帮助我吗? 如果您需要更多详细信息,请询问。

为什么你不能去喜欢的PHPMailer或switmailer库?他们提供对html邮件的全面支持。你也不需要创建所有的头文件。

+0

我自己会选择像梨,但它不是我的虚拟主机提供商,我不换主机商的位置。 – Cornelis 2011-03-31 11:49:53

+0

我没有使用PEAR进行邮寄。为什么你需要交换webhosts?你可以简单地使用这个库,而不需要任何外部依赖。 – Shameer 2011-03-31 11:58:31