简体   繁体   中英

email sending code shows an error in codeigniter

This is email sendind code

function send_letter()

    {

        $description =  $this->input->post('description',true);

        $this->load->model('newsletter_model');

        $this->data['mail_list'] = $this->newsletter_model->getmaillist();

        $this->email->from('ashitha10@gmail.com', 'Imageinit');

        $this->email->subject('Email Test');

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

        foreach($this->data['mail_list'] as $val)

        {

            $this->email->set_newline("\r\n");

            $this->email->to($val['emailid']);

            $this->email->send();

        }  


        $this->index();

    }

The email sends successfully to all emailids..But subject like "nosubject"

Also an error shows

Security:Notice

Message:Undefined Index:subject

FileName:libraries/email.php

line Number:941

content in line 941 in email.php is

if ($this->protocol == 'mail')

        {

            $this->_subject = $this->_headers['Subject'];

            unset($this->_headers['Subject']);
        }

Apparently the code which unset s the Subject header is in the method _write_headers , which is called from _build_message which is called from send . So every time you call send with mail as backend your subject gets erased (but the first mail is okay, since the subject gets stored in $this->_subject but that gets overwritten with an empty string in a subsequent call too).

This seems like a bug in the email.php, but as a workaround you could just set the subject in every iteration of the loop (so just move $this->email->subject('Email Test'); inside of the foreach -loop, maybe after the to -call).

Use this code

function send_letter()

{

    $description =  $this->input->post('description',true);

    $this->load->model('newsletter_model');

    $this->data['mail_list'] = $this->newsletter_model->getmaillist();



    foreach($this->data['mail_list'] as $val)

    {

        $this->email->from('ashitha10@gmail.com', 'Imageinit');

        $this->email->subject('Email Test');

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


        $this->email->set_newline("\r\n");

        $this->email->to($val['emailid']);

        $this->email->send();

    }  


    $this->index();

}

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