php 如何从codeigniter中的视图发送包含内容的电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10663277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:45:32  来源:igfitidea点击:

How to send an email with content from a View in codeigniter

codeigniterphp

提问by imsyedahmed

I want to send an email to user from my application with the content of the email loaded from a view. This is the code i've tried out till now:

我想从我的应用程序向用户发送一封电子邮件,其中包含从视图加载的电子邮件内容 。这是我迄今为止尝试过的代码:

$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();

回答by Niranjan

  1. You need tocall $this->load->library('email');within the controller as well for the email in CI to work.
  2. Also , in your code : $fromemailis not initialized.
  3. You need to have SMTP supporton your server.
  4. $config should be declared as an array beforeassigning values and keys.
  1. 您还需要$this->load->library('email');在控制器内 调用,以便 CI 中的电子邮件工作。
  2. 此外,在您的代码中 : $fromemail未初始化。
  3. 您的服务器需要支持 SMTP
  4. $config 应分配值和键之前声明为数组。

Working Code:

工作代码:

$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();

Edit:$mesg = $this->load->view('template/email',true);should be having the trueas pointed out by lycanian. By setting it as true , it doesn't send data to the output stream but it will return as a string.

编辑:$mesg = $this->load->view('template/email',true);应具有真正的lycanian如指出。通过将其设置为 true ,它不会将数据发送到输出流,但会作为字符串返回。

Edit:$this->load->view();need a second parameter with data or empty like $mesg = $this->load->view(view,data,true);, if not it wont work

编辑:$this->load->view();需要一个带有数据或空的第二个参数$mesg = $this->load->view(view,data,true);,如果没有它就行不通

回答by Rouss88

This line $mesg = $this->load->view('template/email',true);should be like this
$mesg = $this->load->view('template/email','',true);
with the single quotes before the value true, and it will work perfectly

这一行$mesg = $this->load->view('template/email',true); 应该是这样的
$mesg = $this->load->view('template/email','',true);
在值 true 之前使用单引号,它将完美运行

回答by Siddharth Shukla

Email template send In codeigniter we need to put and meta tag before sending email

电子邮件模板发送在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();

回答by illeas

U will try it!! it's working for mine after many errors facing

你会试试的!!在面临许多错误后,它对我有用

            $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();