1
votes

welcome.php file

  function formData(){
        if (empty($this->input->post('checkprocess'))) {
                        $email_config = Array(
                            'mailtype' => 'html',
                            'charset' => 'iso-8859-1'
                        );

                        $admin_email ='[email protected]';
                        $name = $this->input->post('full_name');
                        $email = $this->input->post('email');
                        $message = "Dear Admin, <br/>" .
                        "Contact Information of Users:" .'<br/>'.
                        "<b>Name</b> :- " . $this->input->post('full_name') .'<br/>'.
                        "<b>Email:-</b> " . $this->input->post('email') .'<br/>'.
                        "<b>Contact Number:-</b> " . $this->input->post('mob_num') .'<br/>'.'<br/>'.
                        "<b>Address:-</b> " . $this->input->post('address') .'<br/>'.'<br/>'.
                        "<b>Message:-</b> " . $this->input->post('message');
                        $this->load->library('email', $email_config);
                        $this->email->set_newline("\r\n");
                        $this->email->from($this->input->post('email'), $this->input->post('name'));
                        $this->email->to($admin_email);
                        $this->email->subject("Public Contact/Feedback");
                        $this->email->message($message);

                        if ($this->email->send()) {
                        echo "<meta http-equiv=REFRESH CONTENT=0;url='".base_url()."'>";exit();

                        $this->db->insert('tbl_contactusform',$email_config);
                                    redirect(base_url());
                            }
    }
}

library/email.php file

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

$config = array(
    'protocol'  => 'smtp',
    'smtp_host' => 'ssl://smtp.example.com',
    'smtp_port' => 465,
    'smtp_user' => '**********@gmail.com',
    'smtp_pass' => '************',
    'mailtype'  => 'html',
    'charset'   => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");

$htmlContent = '<h1>Sending email via SMTP server</h1>';
$htmlContent .= '<p>This email has sent via SMTP server from CodeIgniter application.</p>';

$this->email->to('**********@gmail.com');
$this->email->from('**********@gmail.com','MyWebsite');
$this->email->subject('How to send email via SMTP server in CodeIgniter');
$this->email->message($htmlContent);


$this->email->send();

I want to add email feature in website but I get this stack trace:

A PHP Error was encountered Severity: Notice

Message: Undefined property: Welcome::$email

Filename: MX/Loader.php

Line Number: 304

Backtrace:

error and

An uncaught Exception was encountered Type: Error

Message: Call to a member function initialize() on null

Filename: E:\xampp\htdocs\website\application\libraries\email.php

Line Number: 17

I am a beginner in CodeIgniter.

1

1 Answers

2
votes

In order to use email class you have to load the library by using

$this->load->library('email');

or

In application/config/autoload.php

$autoload['libraries'] = array('email');