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.
add this code lines:
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$this->email->set_crlf("\r\n");
Try to manually set the content type header doing this:
$this->email->set_header('Content-Type', 'text/html');
That solve the issue for me.
You can try this line of code which will set the mail type to be for HTML
:
$this->email->set_mailtype("html");
My issue was that Codeigniter's Global XSS Filtering was encoding some html tags like <html>
so the email clients could no longer recognize them.
To get around this, check out my other post.
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 <strong>Example Inc.</strong>')
->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.
To send HTML email you first have to compose your message in a variable and then pass that variable to codeigniter's "$this->email->message()" method, like below,
$this->load->library('email');
$message = "
<html>
<head>
<title>your title</title>
</head>
<body>
<p>Hello Sir,</p>
<p>Your message</p>
</body>
</html>";
$this->email->from('email id', 'name');
$this->email->to('email id');
$this->email->subject('email subject');
$this->email->message($message);
if ($this->email->send()) {
print "success";
} else {
print "Could not send email, please try again later";
}
hope it will help.
enjoy!!