send html mail using codeigniter
Asked Answered
H

13

21

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' => '[email protected]',
        'smtp_pass' => '',
        'mailtype'  => 'html', 
        'charset' => 'utf-8',
        'wordwrap' => TRUE

    );
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $email_body ="<div>hello world</div>";
    $this->email->from('[email protected]', 'ddd');

    $list = array('[email protected]');
    $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?

Herbart answered 1/2, 2012 at 12:8 Comment(2)
I don't think ssl:// is part of a host name. Maybe there's another setting to turn on SSL?Condiment
codeigniter full html email configuration: https://mcmap.net/q/658465/-email-showing-html-source-when-i-send-email-using-codignator-email-classCaaba
C
78

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

 $this->email->set_mailtype("html");
Cabot answered 1/2, 2012 at 19:29 Comment(1)
no accepted answers yet?Scorify
R
25

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('[email protected]', 'Example Inc.')
    ->to('[email protected]')
    ->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.

Respire answered 24/6, 2015 at 10:10 Comment(1)
I already tried this way. Email sent successfully. but the problem is images inside my html body is not visible. all the text and styles applied. but no images. what is the issue?Huck
P
3

Setting the mail type to HTML works for me:

$email_setting  = array('mailtype'=>'html');
$this->email->initialize($email_setting);
Polycarp answered 18/3, 2013 at 13:11 Comment(0)
F
3

Try to manually set the content type header doing this:

$this->email->set_header('Content-Type', 'text/html');

That solve the issue for me.

Foreordain answered 30/4, 2018 at 8:41 Comment(0)
I
3

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!!

Intertwist answered 21/5, 2018 at 8:37 Comment(0)
B
2

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' => '[email protected]', //[email protected]
        '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);
    }
Belloir answered 24/5, 2017 at 10:36 Comment(0)
J
1

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

Judd answered 27/12, 2017 at 17:55 Comment(0)
H
1

add this code lines:

$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$this->email->set_crlf("\r\n");
Housecarl answered 8/4, 2020 at 20:20 Comment(1)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Assiduity
T
0

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.

Tater answered 17/7, 2012 at 16:8 Comment(0)
S
0

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();
Speakeasy answered 27/12, 2017 at 10:7 Comment(0)
S
0
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Johny</td><td>10th</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';

$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://mail.example.com',
'smtp_port' => 465,
'smtp_user' => '[email protected]',
'smtp_pass' => 'example@123',
'mailtype'  => 'html',
'charset'   => 'iso-8859-1'
);

$this->load->library('email', $config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$this->email->from('[email protected]', 'example');
$this->email->to('[email protected]');
$this->email->subject('Enquiry');
$this->email->message($message);
Spiritism answered 10/4, 2020 at 14:45 Comment(1)
This answer may be correct, but you should elaborate on how your example code crucially differs from the one in the question. (I'd guess the important difference is the set_mailtype("html"); line in your code, which is missing from the code in the question.)Terzetto
T
0

PHP code/CodeIgniter :

$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com'; //smtp host name
$config['smtp_port'] = '465'; //smtp port number
$config['smtp_user'] = $from_email;
$config['smtp_pass'] = "password_addhere"; //$from_email password
$config['mailtype'] = "html";
// $config['charset'] = "iso-8859-1";
$config['wordwrap'] = TRUE;
// $config['newline'] = "\r\n"; //use double quotes
// $this->email->initialize($config);
$this->load->library("email", $config);
//send mail
$this->email->from($from_email, 'ABC OWNER');
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($this->load->view('mails/contact',$data, true));
$this->email->set_mailtype("html");

return $this->email->send();

VIEW FILE in HTML :

<html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Contact Form</title> 
        </head> 
        <body> 
            <h1>New user contacted for information</h1> 
            <table cellspacing="0" style="border: 2px  #000; width: 100%;"> 
                <tr> 
                    <th>Customer Name:</th><td><?php echo $name; ?><br/></td> 
                </tr> 
                <tr style="background-color: #e0e0e0;"> 
                    <th>Email:</th><td><?php echo $email; ?><br/></td> 
                </tr> 
                <tr> 
                    <th>Phone:</th><td><?php echo $phone; ?><br></td> 
                </tr> 
                <tr> 
                    <th>Message:</th><td><?php echo $message; ?><br></td> 
            </tr> 
            </table> 
        </body> 
        </html>
Teeming answered 27/6, 2022 at 14:14 Comment(0)
C
-2

U will try it!! it's working for mine after showing many errors it's 100% working.

        $subject = 'New message.';
        $config = Array(        
            'protocol' => 'sendmail',
            'smtp_host' => 'Your smtp host',
            'smtp_port' => 465,
            'smtp_user' => 'webmail',
            'smtp_pass' => 'webmail pass',
            'smtp_timeout' => '4',
            'mailtype'  => 'html', 
            'charset'   => 'utf-8',
            'wordwrap' => TRUE
        );
        $this->load->library('email', $config);
        $this->email->set_newline("\r\n");
        $this->email->set_header('MIME-Version', '1.0; charset=utf-8');
        $this->email->set_header('Content-type', 'text/html');

        $this->email->from('from mail address', 'Company name ');
        $data = array(
             'message'=> $this->input->post('message')
                 );
        $this->email->to($toEmail);  
        $this->email->subject($subject); 

        $body = $this->load->view('email/sendmail.php',$data,TRUE);
        $this->email->message($body);   
        $this->email->send();
Capon answered 3/5, 2018 at 18:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.