วิธีส่งอีเมลพร้อมเนื้อหาจาก View ใน Codeigniter

ฉันต้องการส่งอีเมลถึงผู้ใช้จากแอปพลิเคชันของฉันโดยมีเนื้อหาของอีเมลที่โหลดจาก มุมมอง นี่คือรหัสที่ฉันได้ลองจนถึงตอนนี้:

$toemail = "[email protected]";

$subject = "Mail Subject is here";
$mesg = $this->load->view('template/email');

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

$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';

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

$this->email->to($toemail);
$this->email->from($fromemail, "Title");
$this->email->subject($subject);
$this->email->message($mesg);
$mail = $this->email->send();

person Syed Ahmed    schedule 19.05.2012    source แหล่งที่มา
comment
เหตุใดคุณจึงต้องการส่งอีเมลจากมุมมอง คุณสามารถรับข้อมูลในคอนโทรลเลอร์ (จากแบบฟอร์ม ซึ่งสามารถอยู่ในมุมมองหรือจากฐานข้อมูล)   -  person Niranjan    schedule 19.05.2012
comment
ฉันต้องการที่จะไม่ใช่คำถาม   -  person hakre    schedule 19.05.2012
comment
คำถามของฉันคือจะส่งอีเมลพร้อมเนื้อหาจาก View ใน Codeigniter ได้อย่างไร ดูที่ฉันต้องการเป็นคำอธิบายคำถามของฉัน   -  person Syed Ahmed    schedule 20.05.2012


คำตอบ (4)


  1. คุณ ต้อง โทร $this->load->library('email'); ภายในตัวควบคุมเช่นกัน เพื่อให้อีเมลใน CI ทำงาน
  2. นอกจากนี้ ในรหัสของคุณ : $fromemail ยังไม่ได้เตรียมใช้งาน
  3. คุณต้องมี การสนับสนุน SMTP บนเซิร์ฟเวอร์ของคุณ
  4. ควรประกาศ $config เป็นอาร์เรย์ ก่อน กำหนดค่าและคีย์

รหัสการทำงาน:

$this->load->library('email');
$fromemail="[email protected]";
$toemail = "[email protected]";
$subject = "Mail Subject is here";
$data=array();
// $mesg = $this->load->view('template/email',$data,true);
// or
$mesg = $this->load->view('template/email','',true);


$config=array(
'charset'=>'utf-8',
'wordwrap'=> TRUE,
'mailtype' => 'html'
);

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

$this->email->to($toemail);
$this->email->from($fromemail, "Title");
$this->email->subject($subject);
$this->email->message($mesg);
$mail = $this->email->send();

แก้ไข: $mesg = $this->load->view('template/email',true); ควรมี จริง ตามที่ lycanian ชี้ให้เห็น เมื่อตั้งค่าเป็น true จะไม่ส่งข้อมูลไปยังเอาต์พุตสตรีม แต่จะส่งกลับเป็นสตริง

แก้ไข: $this->load->view(); ต้องการพารามิเตอร์ตัวที่สองที่มีข้อมูลหรือว่างเปล่าเช่น $mesg = $this->load->view(view,data,true); หากไม่เป็นเช่นนั้นก็จะไม่ทำงาน

person Niranjan    schedule 19.05.2012
comment
ใช้งานได้: $mesg = $this-›load-›view('Payment/step1', '', TRUE); - person Syed Ahmed; 19.05.2012
comment
@lycanian ไม่อย่างนั้นมันก็สะท้อน ขอบคุณ. - person Niranjan; 19.05.2012
comment
@lycanian ทุกอย่างปกติดีใช่ไหม? ยอมรับสิ่งนี้เป็นวิธีแก้ปัญหาหากโค้ดใช้งานได้ - person Niranjan; 19.05.2012
comment
นี่จำเป็นต้องโหลดไลบรารี่ $this->load->library('email'); สองครั้งจริง ๆ หรือไม่ - person M B Parvez Rony; 15.10.2015
comment
จะทำสิ่งนี้ $mesg = $this->load->view('template/email','',true); ใน Codeigniter 4 ได้อย่างไร? - person Niby; 24.08.2020

บรรทัดนี้ $mesg = $this->load->view('template/email',true); ควรเป็นเช่นนี้
$mesg = $this->load-> view('template/email','',true);
ด้วยเครื่องหมายคำพูดเดี่ยวก่อนค่าจริง และจะทำงานได้อย่างสมบูรณ์แบบ

person Rouss88    schedule 18.07.2013
comment
นี่ควรเป็นคำตอบยกเว้นสำหรับคำถาม ellislab.com/codeigniter/user-guide /ทั่วไป/views.html - person Tim Wickstrom; 26.02.2014

เทมเพลตอีเมลส่ง ใน Codeigniter เราจำเป็นต้องใส่และเมตาแท็กก่อนที่จะส่งอีเมล

$this->data['data'] = $data;
$message = $this->load->view('emailer/create-account', $this->data,  TRUE);
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');
$this->email->from($email, $name);
$this->email->to('[email protected]');
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
person Siddharth Shukla    schedule 02.03.2017
comment
แม้ว่ารหัสนี้อาจตอบคำถามได้ แต่การให้บริบทเพิ่มเติมเกี่ยวกับวิธีการและ/หรือสาเหตุที่รหัสนี้สามารถแก้ไขปัญหาได้จะช่วยเพิ่มมูลค่าในระยะยาวของคำตอบได้ - person Donald Duck; 02.03.2017

คุณจะลอง!! มันใช้งานได้สำหรับฉันหลังจากเผชิญกับข้อผิดพลาดมากมาย

            $subject = 'New message.';
            $config = Array(        
                'protocol' => 'sendmail',
                'smtp_host' => 'Your smtp host',
                'smtp_port' => 465,
                'smtp_user' => 'webmail',
                'smtp_pass' => 'webmail pass',
                'smtp_timeout' => '4',
                'mailtype'  => 'html', 
                'charset'   => 'utf-8',
                'wordwrap' => TRUE
            );
            $this->load->library('email', $config);
            $this->email->set_newline("\r\n");
            $this->email->set_header('MIME-Version', '1.0; charset=utf-8');
            $this->email->set_header('Content-type', 'text/html');

            $this->email->from('from mail address', 'Company name ');
            $data = array(
                 'message'=> $this->input->post('message')
                     );
            $this->email->to($toEmail);  
            $this->email->subject($subject); 

            $body = $this->load->view('email/sendmail.php',$data,TRUE);
            $this->email->message($body);   
            $this->email->send();
person illeas    schedule 03.05.2018