Sanjuktha
Sanjuktha

Reputation: 1085

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;" />')

Upvotes: 4

Views: 14738

Answers (3)

Sahib Khan
Sahib Khan

Reputation: 602

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.

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.

Upvotes: 3

Sanjuktha
Sanjuktha

Reputation: 1085

Thanks abdulla for the help. I tried in different way as below.

$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('[email protected]', 'Admin');
    $this->email->to('[email protected]'); 

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

     $this->email->send(); 

Upvotes: 1

Abdulla Nilam
Abdulla Nilam

Reputation: 38652

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

Other things will work as well such as ...src=", ...href=", url('')

Method 02 (standard)

$to = '[email protected]';
$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)

Upvotes: 5

Related Questions