简体   繁体   中英

HTML Template message in codeigniter email

I am using codeigniter email library to send email. My message contains html code. but couldn't send email, instead it shows error like "This message is rejected by our SPAM filters". here is my code :

email-template.php

<html>
<body>
    <div style="background-color: #f3f3f3; max-width: 650px; margin: 0 auto; box-shadow: 0px 0px 25px rgba(0, 0, 0, 0.25);">
        <!-- header section -->
        <div style="/*border-bottom: 5px solid #cc342f;*/ border-bottom: 5px solid #fa233d; background-color: #f4f1f1; padding: 20px;">
            <div style="text-align: left;">
                <img src="<?php echo base_url()?>assets/images/logo.png" alt="test">
            </div>  
        </div>
        <!-- content section -->
        <div style="padding: 20px 25px; text-align: justify; overflow: hidden; position: relative;">        
            <p style="font-family: Arial, Helvetica, sans-serif; font-weight: bold; color: #fa233d;">
                    Hi <?php echo $user;?>,
            </p>
            <p style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: 100; color: #1b1b1b; line-height: 24px;">
                You recently requested to reset your password for your ***** account.
            </p>
            <p style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: 100; color: #1b1b1b; line-height: 24px;">
                Click the button below to reset your password.
            </p>
            <p style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: 100; color: #1b1b1b; line-height: 24px;">
                <a href="">Reset Password</a>
            </p>
            <p style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: 100; color: #1b1b1b; line-height: 24px;">
                If you did not request a password reset, please ignore this mail. 
            </p>
            <p style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: 100; color: #1b1b1b; line-height: 24px;">
                Thanks,
            </p>
            <p style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: 100; color: #1b1b1b; line-height: 24px;">
                *********
            </p>

            <!-- Disclaimer -->
            <p style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: 100; color: #1b1b1b; line-height: 14px; border: 1px solid #ddd; padding: 10px">
                <small>
                    If you have trouble clicking the reset button, copy and paste below URL in your web browser. <br/>
                    <a href="<?php echo base_url().'Home/resetPassword/'.$email.'/'.$password?>">Reset Password</a>

                </small>
            </p>

        </div>  

        <!-- footer section -->
        <div style="border-top: 3px solid #fa233d; background-color: #f4f1f1; padding: 10px 25px; text-align: right">

            <p style="margin: 0; font-family: Arial, Helvetica, sans-serif; font-size: 11px; color: #1d1d1d; text-decoration: none; display: inline-block;">+91 1231 123123&nbsp; <span style="color: #fa233d;">|</span> &nbsp;+91 123123 12313 </p>

            <a href="<?php echo base_url()?>" style="font-family: Arial, Helvetica, sans-serif; font-size: 11px; color: #1d1d1d; text-decoration: none; display: inline-block;">&nbsp; <span style="color: #fa233d;">|</span> &nbsp;Visit Us</a>

        </div>
    </div>
</body>

home.php controller

public function forgotPassword() { 
    $email = $this->input->post('email');    
    $password = ****** 
    $user = $this->Home_model->findUsernameByEmail($email);
    $data = array('email' => $email,
                    'password' => $password,
                    'user' => $user);
    $body = $this->load->view('templates/forgot-password', $data, true);
    $result = $this->Home_model->forgotPassword($email, $body);    
    echo $result;
}

and Home_model.php

function forgotPassword($email, $message){ //print_r( $message); exit;
    $this->db->where('LoginEmailID', $email);
    $result = $this->db->get('usermaster_tbl')->row_array(); 
    if($result['UserID'] != 0 && $result['UserID'] != ''){                
        $this->email->message($message);
        $this->email->to($email);
        $this->email->from('****@****.in', 'Administrator');
        $this->email->subject('Reset Password');
        $this->email->send();
        print_r($this->email->print_debugger());
        return 1;            
    }
    else{            
        return 0;
    }

}

if i use message other than html code, it is working. But if html template used, then mail is not sent

Add mailtype in your config

$config = Array(
'mailtype' => 'html',
...etc...
);

See http://codeigniter.com/user_guide/libraries/email.html

1- In your configuration file located in Applications / config / email.php you must put the following

 $config['protocol'] = 'smtp';
 $config['charset'] = 'utf8';
 $config['wordwrap'] = TRUE;
 $config['smtp_host'] = 'ssl://smtp.googlemail.com';
 $config['smtp_port'] = 465;
 $config['smtp_user'] = "youemail@gmail.com";
 $config['smtp_pass'] = "yourpass";
 $config['newline'] = "\r\n";
 $config['priority'] = 1;
 $config['mailtype'] = 'html'; //very important to send email html

2 - In order for your emails not to be detected as spam you must configure the from with the same mail that you put in the configuration file email.php

$this->email->from('myemail@gmail.com', 'Administrator');

I found my answer. Actually the problem is with url's used in template for image and anchor. I'm testing it with local server, so the link been blocked by mail server security. I changed it to live site url. Now the problem solved.

Thank you all for your valuable suggestions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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