简体   繁体   English

如何在Codeigniter中的电子邮件中显示图像?

[英]How to display an image in email in codeigniter?

      $this->load->library('upload');
      $this->load->library('email');
      $this->email->set_newline("\r\n");
      $this->email->from($email);
      $this->email->to($cus_email);
      $this->email->subject($promo_name.' Offers');
      $this->email->message($remarks.'/n <img src="<?php echo base_url();?>images/promotions/<? echo $image; ?>" style="float: left;margin-top: -11px;width: 100px;margin-left:10px;" />');


     // $this->email->attach($img,'inline');
    // $this->email->attach($img);
       $this->email->send();

Tried including in message and also tried as inline attachment,but both didn't work. 尝试在消息中包含在内,还尝试作为内联附件,但两者均无效。 What i need is when an email is sent & on opening it,the image should be seen. 我需要的是在发送电子邮件并打开它时,应该可以看到该图像。

Currently I am receiving email as given below 目前,我收到以下电子邮件

$remarks.'/n <img src="<?php echo base_url();?>images/promotions/<? echo $image; ?>" style="float: left;margin-top: -11px;width: 100px;margin-left:10px;" />')

Method 01 (Codeigniter) 方法01(Codeigniter)

Upload the image to your server. 将图像上传到您的服务器。

In your controller that is sending email , add this line: 发送电子邮件的控制器中 ,添加以下行:

$this->email->attach("/home/yoursite/location-of-file.jpg", "inline");

Then in the email view add this: 然后在电子邮件视图中添加以下内容:

<img src="cid:location-of-file.png" border="0">

And location-of-file.jpg will be changed to the content id for that resource. 并且location-of-file.jpg将更改为该资源的内容ID。

Other things will work as well such as ...src=" , ...href=" , url('') 其他功能也可以使用,例如...src="...href="url('')

Method 02 (standard) 方法02(标准)

$to = 'receiver@gmail.com';
$subject = 'Mail With Inline Image';

$message = 'This is first line';
$message .= 'This is Second line';
$message .= '<img src="http://example.com/images/filename.jpg" alt="my picture">';

$header = 'MIME-Version: 1.0';
$header .= 'Content-Type: text/html; charset="ISO-8859-1';

mail($to, $subject,$message,$header)

Thanks abdulla for the help. 感谢阿卜杜拉的帮助。 I tried in different way as below. 我尝试了以下不同方式。

$mail_body = $this->load->view('email_template/promo_email', $data, TRUE); $ mail_body = $ this-> load-> view('email_template / promo_email',$ data,TRUE);

    $this->load->library('email');
    $config['mailtype'] = 'html';
    $this->email->initialize($config);

    $this->email->from('another@another-example.com', 'Admin');
    $this->email->to('them@their-example.com'); 

    $this->email->subject($subject.' Promotion');
    $this->email->message($mail_body);

     $this->email->send(); 

I am not sure if the other answers may have ever worked or not but the latest 3.1.9 is little different and I just tested it. 我不确定其他答案是否可行,但是最新的3.1.9几乎没有什么不同,我只是对其进行了测试。

https://www.codeigniter.com/user_guide/libraries/email.html https://www.codeigniter.com/user_guide/libraries/email.html

$filename = '/img/photo1.jpg';
$this->email->attach($filename);
foreach ($list as $address)
{
        $this->email->to($address);
        $cid = $this->email->attachment_cid($filename);
        $this->email->message('<img src="cid:'. $cid .'" alt="photo1" />');
        $this->email->send();
}

File name is the full path to your file on your server. 文件名是服务器上文件的完整路径。 First you attache the file then you get the cid of the file and display it inline anywhere in the email body. 首先,您附加文件,然后获取文件的cid,并将其内联显示在电子邮件正文中的任何位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM