codeigniter电子邮件中的HTML模板消息

时间:2017-10-06 11:49:10

标签: php html codeigniter email

我正在使用codeigniter电子邮件库发送电子邮件。我的消息包含HTML代码。但是无法发送电子邮件,而是显示错误,例如“此消息被我们的垃圾邮件过滤器拒绝”。这是我的代码:

电子邮件的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控制器

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;
}

和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;
    }

}

如果我使用除html代码之外的消息,它正在工作。但如果使用html模板,则不发送邮件

3 个答案:

答案 0 :(得分:0)

在配置中添加mailtype

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

请参阅http://codeigniter.com/user_guide/libraries/email.html

答案 1 :(得分:0)

1-在Applications / config / email.php中的配置文件中,您必须输入以下内容

 $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 - 为了使您的电子邮件不会被检测为垃圾邮件,您必须使用配置文件中的相同邮件配置来自email.php

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

答案 2 :(得分:0)

我找到了答案。实际上问题在于url用于模板中的图像和锚点。我正在使用本地服务器对其进行测试,因此该链接已被邮件服务器安全性阻止。我将其更改为直播网站网址。现在问题解决了。

谢谢大家的宝贵建议。