使用 PHP 嵌入图像以在电子邮件中使用?

发布于 2024-09-07 03:44:21 字数 262 浏览 1 评论 0原文

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

我还应该担心这个吗?通过网络上图像的链接加载它们是否更有利?我认为这会减轻我的服务器的一些负载,因此不必在每封打开的电子邮件中直接从我的网站下载它,从而稍微减少带宽。

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

I know how to embed an image into an email as an attachment, but that's not really what I want to do. What I want to do is embed an image into the message and use that within the body of the email message itself. Is it possible to do this or possibly reference an attached file somehow to be used in the message?

Should I even worry about this? Is it more beneficial to just load them via a link to the image on my network? I was thinking that this would take some load off my server so it didn't have to be downloaded directly from my site in every email message that got opened, thus reducing bandwidth a little.

Note: I'm not looking for any email frameworks or libraries etc, just for a simple way to accomplish this task.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

影子是时光的心 2024-09-14 03:44:21

人们会插话告诉你使用 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);

}

至于是否值得这样做是另一个问题。如果您附加图像,则您将使用带宽来发送消息。也许一次使用所有带宽比分散使用带宽更好?也许不是。

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

People will chime in to tell you to use SwiftMailer or some other library. But it's not that hard. It's really finicky, but not hard. Below is a function I use. I've anonymized it a bit, and hopefully didn't break anything. In the html message (which I get from another function), link to your attached image this way:

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

Where $linkID is the same value used in the function below.

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);

}

Whether or not it's worthwhile to do is another question. If you attach the image, you're using bandwidth to send the message out. Maybe it's better for you to use the bandwidth all at once rather than spread out? Maybe not.

I didn't use attached images until I had to send out a different image to each recipient. I wasn't about to store and manage all of those images on our server (and try to uniquely name them).

你又不是我 2024-09-14 03:44:21
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--

边界、字符集、内容传输编码当然都是可以选择的。 PHPMailer 软件包可以自动为您执行此操作: http:// /www.ustrem.org/en/article-send-mail-using-phpmailer-en/

另请参阅以下位置的图像:http://mailformat.dan.info/body/html.html

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 of course all choosable. The PHPMailer package can do this automatically for you: http://www.ustrem.org/en/article-send-mail-using-phpmailer-en/

See also including images at: http://mailformat.dan.info/body/html.html

我不是你的备胎 2024-09-14 03:44:21

我认为唯一的方法是通过 url 引用图像,如下所示:

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

I think the only way to do this is to reference a image by url like this:

<img src="http://www.youwebsite.com/yourimage.jpg" alt="This is a description" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文