使用PHP嵌入用于电子邮件消息的图像?

时间:2010-06-22 22:37:14

标签: php image email attachment embedding

我知道如何将图像作为附件嵌入到电子邮件中,但这不是我想要做的。我想要做的是将图像嵌入到消息中,并在电子邮件消息本身中使用它。是否可以这样做或者可能以某种方式引用附加文件以在消息中使用?

我应该担心这个吗?通过链接到我网络上的图像来加载它们会更有益吗?我当时认为这会占用我服务器的一些负载,所以不必在每个打开的电子邮件中直接从我的网站下载,从而减少带宽。

注意:我不是在寻找任何电子邮件框架或库等,只是为了完成此任务的简单方法。

3 个答案:

答案 0 :(得分:3)

人们会告诉你使用SwiftMailer或其他一些库。但这并不难。这真的很挑剔,但并不难。以下是我使用的功能。我已经对它进行了匿名处理,希望没有破坏任何东西。在html消息(我从另一个函数中获取)中,以这种方式链接到您附加的图像:

$output .= '<p><img src="cid:' . $linkID . '" alt="graph" /></p>';

$linkID与下面函数中使用的值相同。

public function sendEmail($emailAddress)
{
    $random_hash = md5(date('r', time()));

    $htmlString = $this->getHTML($random_hash);

    $message = "--mixed-$random_hash\n";
    $message .= 'Content-Type: multipart/alternative; boundary="alt-' . $random_hash . '"' . "\n\n";
    $message .= 'MIME-Version: 1.0' . "\n";
    $message .= '--alt-' . $random_hash . "\n";
    $message .= 'Content-Type: text/plain; charset="iso-8859-1"' . "\n";
    $message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";

    // text message
    $message .= strip_tags($htmlString) . "\n\n";

    $message .= '--alt-' . $random_hash . "\n";
    $message .= 'Content-Type: text/html; charset="iso-8859-1"' . "\n";
    // $message .= 'MIME-Version: 1.0' . "\n";
    $message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";

    // html message
    $message .= $htmlString . "\n\n";

    $message .= '--alt-' . $random_hash . '--' . "\n";

    // graph image
    if($this->selectedGraph != 0)
    {
        $graphString = $this->getGraph();
        $graphString = chunk_split(base64_encode($graphString));

        $linkID = 'graph-' . $random_hash . '-image';

        $message .= '--mixed-' . $random_hash . "\n";
        $message .= 'MIME-Version: 1.0' . "\n";
        $message .= 'Content-Transfer-Encoding: base64' . "\n";
        $message .= 'Content-ID: ' . $linkID . "\n";
        $message .= 'Content-Type: image/gif; name="graph.gif"' . "\n";
        $message .= 'Content-Disposition: attachment' . "\n\n";

        $message .= $graphString;
        $message .= '--mixed-' . $random_hash . '--' . "\n";

    }
    else
    {
        $message .= '--mixed-' . $random_hash . '--' . "\n";
    }


    $headers = 'From: ' . $this->from. "\r\nReply-To: " . $this->replyto;
    $headers .= "\r\nContent-Type: multipart/related; boundary=\"mixed-" . $random_hash . "\"\r\nMIME-Version: 1.0";

    $flags = '-f ' . BOUNCED_EMAIL_ADDRESS;

    return mail($emailAddress, $this->subject, $message, $headers, $flags);

}

是否值得做是另一个问题。如果您附加图像,则使用带宽发送消息。也许你最好一次性使用带宽而不是分散?也许不是。

在我不得不向每个收件人发送不同的图像之前,我没有使用附加图像。我不打算在我们的服务器上存储和管理所有这些图像(并尝试对它们进行唯一命名)。

答案 1 :(得分:2)

Content-Type: multipart/related;
  type="text/html";
  boundary="Boundary1"


--Boundary1
Content-Type: multipart/alternative;
boundary="Boundary2"

--Boundary2
Content-Type: text/html; charset = "utf-8"
Content-Transfer-Encoding: 8bit

<img src="cid:content_id_from_attachment">

--Boundary2--

--Boundary1
Content-Type: image/jpeg; name="somefilename.jpg"
Content-Transfer-Encoding: base64
Content-ID: <content_id_from_attachment>
Content-Disposition: inline; filename="somefilename.jpg"

//binary data

--Boundary1--

Boundary,charset,Content-Transfer-Encoding当然都可以选择。 PHPMailer包可以自动为您执行此操作:http://www.ustrem.org/en/article-send-mail-using-phpmailer-en/

另请参阅:http://mailformat.dan.info/body/html.html

中的图片

答案 2 :(得分:-4)


我认为唯一的方法是通过这样的网址引用图片:

<img src="http://www.youwebsite.com/yourimage.jpg" alt="This is a description" />
相关问题