发送电子邮件验证链接与php condeigniter

时间:2016-07-04 10:12:57

标签: php codeigniter email

我想使用PHP CodeIgniter向用户发送电子邮件验证链接。

这是我的控制器功能。

public function Sent_Confirmation_Email()
{
    $emailid  = $this->uri->segment(3);

    $verificationLink = base_url() . 'MainController/Confirm_Activation/'.$emailid;

    $msg .= "Please use the link below to activate your account..<br /><br /><br />";

    $msg .= "<a href='".$verificationLink."' target='_blank'>VERIFY EMAIL</a><br /><br /><br />";

    $msg .= "Kind regards,<br />";

    $msg .= "Company Name";         

    if( ! ini_get('date.timezone') )
    {
        date_default_timezone_set('GMT');
    }        

    $config = array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => 'sender@gmail.com',
        'smtp_pass' => 'password'
    );

    $this->load->library('email',$config);
    $this->email->set_newline("\r\n");
    $this->email->isHTML(true);
    $this->email->from("sender@gmail.com");
    $this->email->to("$emailid");
    $this->email->subject("Email Confirmation - Courses and Tutors");
    $this->email->message($msg);

    if($this->email->send())
    {
    $this->session->set_flashdata('msg', 'A confirmation email has been sent to ' . $emailid .'. Please activate your account using the link provided.');
        redirect(base_url() . 'MainController/EConfirmationPage/'.$emailid);
    } else {
        show_error($this->email->print_debugger());
    }
}

请注意,我正在从本地主机发送电子邮件。我收到了电子邮件,但问题是它也显示了html标签。这是我收到的电子邮件:

Please use the link below to activate your account..<br /><br /><br /><a
href='http://localhost/tutorhunt/MainController/Confirm_Activation/fareedshuja@gmail.com'
target='_blank'>VERIFY EMAIL</a><br /><br /><br />Kind regards,<br />Company
Name

2 个答案:

答案 0 :(得分:1)

尝试初始化电子邮件库并添加mailtype,如下所示

    $this->email->initialize(array(
          'protocol'  => 'smtp',
          'smtp_host' => 'ssl://mailserver',
          'smtp_user' => 'user',
          'smtp_pass' => 'password',
          'smtp_port' => 465,
          'crlf'      => "\r\n",
          'newline'   => "\r\n",
          'mailtype'  => 'html',
    ));

答案 1 :(得分:0)

使用电子邮件模板在电子邮件中发送验证链接比将HTML内容添加为邮件并发送更简单。

  • Creare要在电子邮件中发送的变量数组。
  • 加载该电子邮件模板视图并加载该变量数组并正确设置模板中的变量。

以下是代码示例:

$this->email->set_newline("\r\n");
$this->email->isHTML(true);
$this->email->from("sender@gmail.com");
$this->email->to("$emailid");
$this->email->subject("Email Confirmation - Courses and Tutors");   

$template_data = array(
        'verificationLink' => base_url() . 'MainController/Confirm_Activation/'.$emailid,
        'message' => 'Please use the link below to activate your account..',
        'company_name' => 'test company'                            
);

$body = $this->load->view ('your_view.php', ['template_data'=>$template_data], TRUE); //Set the variable in template Properly
$this->email->message ( $body );

if($this->email->send())  
{
    $this->session->set_flashdata('msg', 'A confirmation email has been sent to ' . $emailid .'. Please activate your account using the link provided.');
    redirect(base_url() . 'MainController/EConfirmationPage/'.$emailid);
} else {
    show_error($this->email->print_debugger());
}