send html mail using codeigniter

匿名 (未验证) 提交于 2019-12-03 01:44:01

问题:

Error in mail content using SMTP in codeigniter Actually, my mail is sent with HTML tags and it is showing the HTML tags which is not correct.

$config = Array( 'protocol' => 'smtp',         'smtp_host' => 'ssl://smtp.googlemail.com',         'smtp_port' => 465,         'smtp_user' => 'user@gmail.com',         'smtp_pass' => '',         'mailtype'  => 'html',          'charset' => 'utf-8',         'wordwrap' => TRUE      );     $this->load->library('email', $config);     $this->email->set_newline("\r\n");     $email_body ="
hello world
"; $this->email->from('user@gmail.com', 'ddd'); $list = array('user@gmail.com'); $this->email->to($list); $this->email->subject('Testing Email'); $this->email->message($email_body); $this->email->send(); echo $this->email->print_debugger();

If am sending mail without using SMTP it works fine. What is my mistake?

回答1:

You can try this line of code which will set the mail type to be for HTML:

 $this->email->set_mailtype("html"); 


回答2:

As of CodeIgniter 3.x. There are many features added. This example is almost same with earlier versions, but you can do much more.

Follow the link for documentation.

// load email library $this->load->library('email');  // prepare email $this->email     ->from('info@example.com', 'Example Inc.')     ->to('to@example.com')     ->subject('Hello from Example Inc.')     ->message('Hello, We are Example Inc.')     ->set_mailtype('html');  // send email $this->email->send(); 

If you have template design. You can also include template in message method like this ...

->message($this->load->view('email_template', $data, true)) 

Here, the first parameter is email_template.php in your views directory, second parameter the data to be sent to email template, you can set it '' or array() or [] if not passing any dynamic data and last parameter true make sure, you grab the template data instead output.

Hope this is helpful.



回答3:

Setting the mail type to HTML works for me:

$email_setting  = array('mailtype'=>'html'); $this->email->initialize($email_setting); 


回答4:

Gmail prevent access of your account. You need some changes on your gmail :-

Step : 1

Some apps and devices use less secure sign-in technology, which makes your account more vulnerable. You can turn off access for these apps, which we recommend, or turn on access if you want to use them despite the risks.

Turn on less secure app

Step : 2

Enable IMAP Status
Enable POP Status

Enable IMAP and POP Status



回答5:

My issue was that Codeigniter's Global XSS Filtering was encoding some html tags like so the email clients could no longer recognize them.

To get around this, check out my other post.



回答6:

Can you please try with this code, B'z I can able to send HTML email with this code.

$configemail = Array(         'protocol' => 'smtp',         'smtp_host' => 'ssl://smtp.gmail.com', //mail.webconsort.com         'smtp_port' => 465, //5074         'smtp_user' => 'XXXXXXXX@gmail.com', //tororide@webconsort.com         'smtp_pass' => 'XXXXXXXX', //'T0r0r1d3'         'mailtype'  => 'html',          'charset'   => 'iso-8859-1'     );      $CI =& get_instance();      $CI->load->library('email', $configemail);      $CI->email->initialize($configemail);     $CI->email->set_newline("\r\n");      $CI->email->from($from, $fromName);     $CI->email->to($to);       $CI->email->subject($subject);     $CI->email->message($body);     if ($attachments != null && !empty($attachments)){         foreach($attachments as $a){             $CI->email->attach($a);         }     }      try{         $CI->email->send();         return true;     }     catch (Exception $e){         //var_dump($e);     } 


回答7:

Use it like this.. it works great for me.

$this->load->library('email');  $config['charset'] = 'iso-8859-1';  $config['wordwrap'] = TRUE;  $config['mailtype'] = 'html';  $this->email->initialize($config);  $this->email->from($fromemail);  $this->email->to($toemail);  $this->email->subject('Subject');  $this->email->message($html);  $success=$this->email->send(); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!