使用PHP嵌入图像以用于电子邮件? [英] Embed images for use in email message using PHP?

查看:176
本文介绍了使用PHP嵌入图像以用于电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

人们会通知您使用SwiftMailer或其他一些库。但这不是那么难。这真的很棒,但不难。以下是我使用的功能。我已经匿名了一下,希望没有任何东西。在html消息(我从另一个函数获取)中,以这种方式链接到您附加的图像:

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

其中 $ linkID 是使用相同的值在下面的功能。

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

这篇关于使用PHP嵌入图像以用于电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆