简体   繁体   中英

can't send email from localhost using codeigniter (blank page)

I'm about new at sending email using CodeIgniter. I have searched any tutorial and forum to handle send email using CodeIgniter, but none of them works for me. Hope someone can help.

This is the controller I use:

function send_email() {
        $config = array(
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://smtp.googlemail.com',
            'smtp_port' => 465,
            'smtp_user' => 'xxxx@gmail.com',
            'smtp_pass' => '****',
            'mailtype' => 'text',
            'charset' => 'iso-8859-1',
            'wordwrap' => TRUE
        );

        $message = '';
        $this->load->library('email');
        $this->email->initialize($config);
        $this->email->set_newline("\r\n");
        $this->email->from('xxxx@gmail.com');
        $this->email->to('xxxx@gmail.com');
        $this->email->subjects('Email Testing');
        $this->email->message($message);

        if($this->email->send()) {
            echo 'Email sent!';
        } else {
            show_error($this->email->print_debugger());
        }
    }

I have edited php.ini as forum said, such as:

;For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = /usr/sbin/sendmail

; ... or under UNIX:
;
;   extension=msql.so
extension=php_openssl.dll

I work Apache/2.2.22 (Ubuntu) Server.

And it shows blank page when I try to call the function. Is there any config to edit? Please give your suggestion. Thank you.

Updated:

This is the display_errors and error_reporting I edited in php.ini :

; display_errors
   Default Value: On
   Development Value: On
   Production Value: On

; error_reporting
   Default Value: E_ALL & ~E_NOTICE
   Development Value: E_ALL | E_STRICT
   Production Value: E_ALL & ~E_DEPRECATED

use PHPMailer its easy

$this->load->library('PHPMailer/PHPMailer');
    $mail=new PHPMailer(); //creat PHPMailer object
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->SMTPAuth = true; //needs login information
    $mail->SMTPSecure = "tls"; //specifies tls security
    $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
    $mail->Port = 587; //gmail smtp port

    $dd3=$this->db->query("select * from email where eid=1")->row_array();;
    /******************* Set Your Credentials********************/
    $mail->Username = 'xxxx'; //SMTP gmail address
    $mail->Password = 'xxx'; // SMTP account password
    $mail->From = 'xxx'; //sender's gmail address


    $mail->FromName = "from";
    $mail->AddAddress("to");//receiver's e-mail address
    $mail->Subject = $subject;//e-mail subject
    $mail->Body ="message";//e-mail message



    $mail->WordWrap = 50;
    if(!$mail->Send())
    {
      $status='Message was not sent.';

    }
    else
    {
      $status='Message has been sent.';
     //  echo $status;
    }

try this in codeigniter!!

public function sendEmail($email,$subject,$message)
    {
   $config = Array(
  'protocol' => 'smtp',
  'smtp_host' => 'ssl://smtp.googlemail.com',
  'smtp_port' => 465,
  'smtp_user' => 'xyz@gmail.com', 
  'smtp_pass' => 'password', 
  'mailtype' => 'html',
  'charset' => 'iso-8859-1',
  'wordwrap' => TRUE
);


      $this->load->library('email', $config);
      $this->email->set_newline("\r\n");
      $this->email->from('xyz@gmail.com');
      $this->email->to($email);
      $this->email->subject($subject);
      $this->email->message($message);
      if($this->email->send())
     {
      echo 'Email send.';
     }
     else
    {
     show_error($this->email->print_debugger());
    }

}

public function send_email() 
    {
    $email="abc@gmail.com";
    $subject="Your subject";
    $message="You can write html tags in this message";
    $this->sendEmail($email,$subject,$message);
    }

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