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

Php 如何在HTML电子邮件中嵌入图像?,php,codeigniter,html-email,newsletter,Php,Codeigniter,Html Email,Newsletter,副本: 我用php发送HTML电子邮件。我想在HTML中使用嵌入的图像。可能吗?我尝试过很多不同的方法,但都不管用。有人能帮我吗 谢谢您需要提供图像所在的整个url 例如: 这并不是一件小事,但经过几次尝试是可行的 首先,学习如何构建一封包含正确图像的多部分电子邮件。如果你不能附加图片,它们显然不会出现在电子邮件中。确保将类型设置为multipart/related 其次,了解如何设置cid引用,特别是附件的内容ID标题 第三,把它们粘在一起 在每个步骤中,请执行以下操作: 看看结果 将

副本:

我用php发送HTML电子邮件。我想在HTML中使用嵌入的图像。可能吗?我尝试过很多不同的方法,但都不管用。有人能帮我吗


谢谢

您需要提供图像所在的整个url 例如:


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

首先,学习如何构建一封包含正确图像的多部分电子邮件。如果你不能附加图片,它们显然不会出现在电子邮件中。确保将类型设置为
multipart/related

其次,了解如何设置cid引用,特别是附件的
内容ID
标题

第三,把它们粘在一起

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

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

    • 下面是一种获取字符串变量的方法,无需担心编码问题

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

      我在这里写了一个小教程,并附有一个屏幕截图(这是针对powershell的,但这并不重要):

      再说一遍:


      我发现通过电子邮件发送图像的最佳方式是使用base64编码

      这方面有大量的链接和教程,但以下是Codeigniter的代码:

      /* Load email library and file helper */
      $this->load->library('email');
      $this->load->helper('file');
      
      $this->email->from('whoever@example.com', 'Who Ever'); // Who the email is from
      $this->email->to('youremailhere@example.com'); // 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
      }
      
      /*加载电子邮件库和文件帮助程序*/
      $this->load->library('email');
      $this->load->helper('file');
      $this->email->from($this)whoever@example.com“,”谁曾经“;//这封电子邮件是谁发的
      $this->email->to($this)youremailhere@example.com'); // 电子邮件的收件人是谁
      $image=“path/to/image”;//图像路径
      $fileExt=get_mime_by_extension($image);//电子邮件->消息(“”);//获取文件的内容,并对其进行base64编码
      如果(!$this->email->send()){
      //这里有错误消息
      }否则{
      //发送的消息
      }
      
      我试过这个@BenSwinburne:它不能发送mail@phpcochin:不要自己编写所有代码。获取一些处理构建多部分/MIME电子邮件并支持文件附件的PHP类。这将使你的生活更加轻松,并可能为你节省数小时的工作时间。向你自己发送一封HTML电子邮件(从你的普通电子邮件客户端),并研究收到的邮件的源代码。这段“示例代码”将真正帮助您理解理论。@ThiefMaster您能分享一些示例吗?除了一件事之外,与我的做法完全相同。我总是在我所有的图片上使用style=“display:block”来阻止gmail bug(在一些图片下面添加空格)。考虑将它添加到你的答案中:不管你想写什么样的css,把它们作为内联css。这篇文章是嵌入一个图像,而不是从互联网上访问它。他问如何嵌入图像。。。那不是嵌入的。。。
      /* Load email library and file helper */
      $this->load->library('email');
      $this->load->helper('file');
      
      $this->email->from('whoever@example.com', 'Who Ever'); // Who the email is from
      $this->email->to('youremailhere@example.com'); // 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
      }