如何在 HTML 电子邮件中嵌入图像?

发布于 2024-12-27 08:54:53 字数 624 浏览 2 评论 0原文

重复:

如何在电子邮件中嵌入图像

如何在 html 电子邮件中嵌入图像

使用 PHP 嵌入图像以在电子邮件中使用?< /a>

我是使用 php 发送 HTML 电子邮件。我想在 HTML 中使用嵌入图像。是否可以?我尝试了很多不同的方法,但没有一个有效。有人可以帮助我吗?

谢谢

Duplicates:

How to embed images in email

How to embed images in html email

Embed images for use in email message using PHP?

I am sending HTML emails using php. I want to use embedded images in the HTML. Is it possible? I have tried lot of different methods, but none are working. Is anyone able to help me please?

Thanks

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

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

发布评论

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

评论(4

归属感 2025-01-03 08:54:53

您需要提供图像所在的完整网址
例子:

<img src='http://www.mydomain.com/imagefolder/image.jpg' alt='my-image' width='' height=''>

You need to provide the whole url where your image resides
example:

<img src='http://www.mydomain.com/imagefolder/image.jpg' alt='my-image' width='' height=''>
长安忆 2025-01-03 08:54:53

这并不是一件小事,但经过几次尝试是可行的。

首先,了解如何构建包含正确图像的多部分电子邮件。如果您无法附加图像,它们显然不会出现在电子邮件中。确保将类型设置为multipart/lated

其次,了解如何设置 cid 引用,特别是附件的 Content-ID 标头。

第三,将它们粘在一起。

在每个步骤中,执行以下操作:

  • 查看结果
  • 发送电子邮件给自己,并将其与您收到的内容进行比较
  • 与工作示例电子邮件

This isn't really trivial, but with a couple of tries doable.

First of all, learn how to build a multipart email, that has the correct images attached to it. If you can't attach the images, they obviously won't be in the email. Make sure to set the type to multipart/related.

Secondly, find out how to set the cid references, in particular the Content-ID header of the attachment.

Third, glue it all together.

At each step, do the following:

  • look at the result
  • send the email to yourself, and compare it to what you received
  • compare it to a working example email
我的奇迹 2025-01-03 08:54:53

我发现通过电子邮件发送图像的最佳方法是使用 base64 对其进行编码。

有大量关于此的链接和教程,但这里是 Codeigniter 的代码应该足够了:

/* Load email library and file helper */
$this->load->library('email');
$this->load->helper('file');

$this->email->from('[email protected]', 'Who Ever'); // Who the email is from
$this->email->to('[email protected]'); // Who the email is to

$image = "path/to/image"; // image path

$fileExt = get_mime_by_extension($image); // <- what the file helper is used for (to get the mime type)

$this->email->message('<img src="data:'.$fileExt.';base64,'.base64_encode(file_get_contents($image)).'" alt="Test Image" />'); // Get the content of the file, and base64 encode it

if( ! $this->email->send()) {
    // Error message here
} else {
    // Message sent
}

I find the best way to send images via email is to encode them with base64.

There are loads of links and tutorials on this but here is this code for Codeigniter should suffice:

/* Load email library and file helper */
$this->load->library('email');
$this->load->helper('file');

$this->email->from('[email protected]', 'Who Ever'); // Who the email is from
$this->email->to('[email protected]'); // Who the email is to

$image = "path/to/image"; // image path

$fileExt = get_mime_by_extension($image); // <- what the file helper is used for (to get the mime type)

$this->email->message('<img src="data:'.$fileExt.';base64,'.base64_encode(file_get_contents($image)).'" alt="Test Image" />'); // Get the content of the file, and base64 encode it

if( ! $this->email->send()) {
    // Error message here
} else {
    // Message sent
}
蘑菇王子 2025-01-03 08:54:53

这是一种无需担心编码即可获取字符串变量的方法。

如果您有 Mozilla Thunderbird,您可以使用它来为您获取 html 图像代码。

我在这里写了一个小教程,并附有屏幕截图(用于 powershell,但这对此无关紧要):

powershell 电子邮件,其中包含显示红色 x 的 html 图片

再次:

如何在电子邮件中嵌入图像

Here is a way to get a string variable without having to worry about the coding.

If you have Mozilla Thunderbird, you can use it to fetch the html image code for you.

I wrote a little tutorial here, complete with a screenshot (it's for powershell, but that doesn't matter for this):

powershell email with html picture showing red x

And again:

How to embed images in email

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文