使用codeigniter项目发送电子邮件

时间:2014-11-28 10:33:26

标签: codeigniter email

我使用以下功能在我的codeigniter项目中发送电子邮件

public function sendMail()
{
 $this->load->library('email',array('mail_type'=>'html'));

        $this->email->from('waaanjula@gmail.com',"Anjula");
        //$this->email->to($this->input->post('inputEmail'));
        $this->email->to('waaanjula@gmail.com');
        $this->email->subject('Confirm your Registration');

        $message = "<p> Thank you for Registering with us</p>";
        $message.="<p><a href='http://localhost/Registration/index.php/login/register_user/'>Click here</a> </p>";

        $this->email->message($message);

        if($this->email->send()){
            echo "Email has been successfully send ";
        }else{
            echo "Error sending email";
        }
}

它提供了&#34;电子邮件已成功发送&#34;但我没有收到任何邮件。请对此提出任何建议。

1 个答案:

答案 0 :(得分:1)

我不会在localhost上工作,将它部署在smtp工作的服务器上并在那里进行测试。

从localhost发送电子邮件的另一种方法是使用另一个smtp服务器,但我建议只在服务器上测试php的mail()函数,因为LOCAL HOST不起作用..

1)下载PHPMailer
2)解压到php项目中的文件夹并将其重命名为phpmailer
3)创建gmail-sample.php并粘贴以下代码:

<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();

// ---------- adjust these lines ---------------------------------------
$mail->Username = "your.username@gmail.com"; // your GMail user name
$mail->Password = "your-gmail-password"; 
$mail->AddAddress("friends.email@domain.com"); // recipients email
$mail->FromName = "your name"; // readable name

$mail->Subject = "Subject title";
$mail->Body    = "Here is the message you want to send to your friend."; 
//-----------------------------------------------------------------------

$mail->Host = "ssl://smtp.gmail.com"; // GMail
$mail->Port = 465;
$mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->From = $mail->Username;
if(!$mail->Send())
    echo "Mailer Error: " . $mail->ErrorInfo;
else
    echo "Message has been sent";
?>

4)从浏览器发送邮件(例如http://localhost/your-project/gmail-sample.php)。