Error when sending smtp email with google and code

2019-09-05 03:03发布

trying to make a reset password function for my website however I cant get past sending an email without this error occuring.

Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.

I am using gmail as the host to send the email. Here is the part of the function that is used to send the email.

$user_email = $this->input->post('email_address');

    $query = $this->db->get_where('account', array('email_address' => $user_email));
    if($query) {
        $config['protocal'] = 'smtp';
        $config['mail_path'] = 'ssl://smtp.googlemail.com';
        $config['smtp_host'] = 'ssl://smtp.googlemail.com';
        $config['smtp_port'] = '465';
        $config['smtp_user'] = 'USEREMAIL';
        $config['smtp_pass'] = 'PASSWORD';
        $config['charset'] = "utf-8";
        $config['mailtype'] = "html";
        $config['newline'] = "\r\n";

        $this->email->initialize($config);

        $this->email->from('matthew.attanasio135@gmail.com', 'Matthew');
        $this->email->to($user_email); 

        $this->email->subject('Email Test');
        $this->email->message('<h1>Testing the email class.<h1>');  

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

I am also getting this error::

Message: Undefined index: Subject

I do not understand why this is happening could you please help me out thank you.

3条回答
Viruses.
2楼-- · 2019-09-05 03:29

try this

    $config = array('auth' => 'login',
        'username' => '***@gmail.com',
        'password' => '***password',
        'port' => '465',
        'ssl' => 'ssl');


    $request = $this->getRequest();


    if ($this->getRequest()->isPost()) {
        if ($form->isValid($request->getPost())) {
            try {
                $smtpHost = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
                $mail = new Zend_Mail();
                $mail->setBodyText($form->getValue('body'));
                $mail->setBodyHtml('');
                $mail->setFrom();
                $mail->addTo());
                $mail->setSubject('');
                $mail->send($smtpHost);
            } catch (Exception $e) {
                die($e);
            }
        }
    }
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-09-05 03:46

You're trying to send the email twice, the first time all your options are set and the second they aren't

change

    $this->email->send();
    if ( ! $this->email->send()) {
        show_error($this->email->print_debugger());
    }

to

    if ( ! $this->email->send()) {
        show_error($this->email->print_debugger());
    } 

You should then get relevant errors if any remain.

Edit:

also change $config['protocal'] to $config['protocol'] to fix the sending problem

查看更多
冷血范
4楼-- · 2019-09-05 03:50

Can you give the rest of the function you're using to send the email, everything you've posted looks correct... the Message: Undefined index: Subject is coming from somewhere else and might be causing problems.

Also... this may seem obvious, but you have actually loaded the email class somewhere, right ($this->load->library('email);)... rather than just initialized it?

查看更多
登录 后发表回答