php Codeigniter $this->email->send() 不起作用,而 mail() 起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17982704/
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
Codeigniter $this->email->send() not working while mail() does
提问by Mr.Web
I can't figure out why if I try to use the CI Email Class it doesn't send emails, while if I use the native PHP mail() Class works.
我不明白为什么如果我尝试使用 CI 电子邮件类它不会发送电子邮件,而如果我使用本机 PHP mail() 类工作。
Has to be noted that sometimes I get "email sent" while is not actually sent and sometimes I get the error "my server is not setup".
必须注意,有时我会收到“电子邮件已发送”而实际上并未发送,有时我会收到错误“我的服务器未设置”。
I tried to figure out how to set it up but... nothing...
我试图弄清楚如何设置它,但......没有......
Controller code follows:
控制器代码如下:
if($this->form_validation->run()){
//Set Language
$this->lang->load('site', $this->session->userdata('lang'));
//Random key
$user_valid_key = md5(uniqid());
//Prepare email
$this->load->library('email', array('mailtype' => 'html'));
$this->email->from($this->config->item('email_signup_from'), 'Wondermark.net');
$this->email->to($this->input->post('email'));
$this->email->subject($this->lang->line('email_signup_subject'));
//Format mail con %s per inserire i campi necessari
$signup_msg = sprintf($this->lang->line('email_signup_message'), $this->input->post('fname'), base_url().'main/signup_confirm/'.$user_valid_key);
$this->email->message((string)$signup_msg);
if($this->email->send()){
//TODO: load view...
echo "email sent";
}else{
$to = $this->input->post('email');
mail($to, 'test', 'Other sent option failed');
echo $this->input->post('email');
show_error($this->email->print_debugger());
}
//TODO: Add to db
}else{
// Form validation failed
}
回答by Anand
Use this setup email..
使用此设置电子邮件..
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = '[email protected]';
$config['smtp_pass'] = 'password';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not
$this->email->initialize($config);
$this->email->from('[email protected]', 'sender_name');
$this->email->to('[email protected]');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();
回答by amisha
I faced this problem and found the following solution. Just a little change in the email config and it's working 100%:
我遇到了这个问题并找到了以下解决方案。只需在电子邮件配置中稍作更改,即可 100% 正常工作:
$config['protocol'] = 'ssmtp';
$config['smtp_host'] = 'ssl://ssmtp.gmail.com';
回答by sonu prajapati
I am using much time Run my configure code in localhostbut it always gives me an error (Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.)
我花了很多时间在localhost 中运行我的配置代码,但它总是给我一个错误(无法使用 PHP SMTP 发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。)
But when run this Below code in my serverit works for me.
但是当在我的服务器中运行下面的代码时,它对我有用。
application>controller>Sendingemail_Controller.php
应用程序>控制器>Sendingemail_Controller.php
public function send_mail() {
$this->load->library('email');
$config = array();
$config['protocol'] = "smtp"; // you can use 'mail' instead of 'sendmail or smtp'
$config['smtp_host'] = "ssl://smtp.googlemail.com";// you can use 'smtp.googlemail.com' or 'smtp.gmail.com' instead of 'ssl://smtp.googlemail.com'
$config['smtp_user'] = "[email protected]"; // client email gmail id
$config['smtp_pass'] = "******"; // client password
$config['smtp_port'] = 465;
$config['smtp_crypto'] = 'ssl';
$config['smtp_timeout'] = "";
$config['mailtype'] = "html";
$config['charset'] = "iso-8859-1";
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$config['validate'] = FALSE;
$this->load->library('email', $config); // intializing email library, whitch is defiend in system
$this->email->set_newline("\r\n"); // comuplsory line attechment because codeIgniter interacts with the SMTP server with regards to line break
$from_email = $this->input->post('f_email'); // sender email, coming from my view page
$to_email = $this->input->post('email'); // reciever email, coming from my view page
//Load email library
$this->email->from($from_email);
$this->email->to($to_email);
$this->email->subject('Send Email Codeigniter');
$this->email->message('The email send using codeigniter library'); // we can use html tag also beacause use $config['mailtype'] = 'HTML'
//Send mail
if($this->email->send()){
$this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
echo "email_sent";
}
else{
echo "email_not_sent";
echo $this->email->print_debugger(); // If any error come, its run
}
}
and my view page where I defined f_emailand emailcomes through post method. application>view>emailtesting.php
和我定义f_email和电子邮件的视图页面来自 post 方法。 应用程序>查看>emailtesting.php
<html>
<head>
<title> Send Email Codeigniter </title>
</head>
<body>
<?php
echo $this->session->flashdata('email_sent');
echo form_open('/Sendingemail_Controller/send_mail');
?>
<input type = "email" name = "f_email" placeholder="sender email id (from)" required />
<input type = "email" name = "email" placeholder="reciever email id (to)" required />
<input type = "submit" value = "SEND MAIL">
<?php
echo form_close();
?>
</body>
if some error comes again please visit official documentation below:
如果再次出现错误,请访问以下官方文档:
回答by Tim
Codeigniter User Guide: https://www.codeigniter.com/user_guide/libraries/email.html
Codeigniter 用户指南:https://www.codeigniter.com/user_guide/libraries/email.html
This setup works for me:
这个设置对我有用:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'Your SMTP Server',
'smtp_port' => 25,
'smtp_user' => 'Your SMTP User',
'smtp_pass' => 'Your SMTP Pass',
'mailtype' => 'html'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
//Add file directory if you need to attach a file
$this->email->attach($file_dir_name);
$this->email->from('Sending Email', 'Sending Name');
$this->email->to('Recieving Email address');
$this->email->subject('Email Subject');
$this->email->message('Email Message');
if($this->email->send()){
//Success email Sent
echo $this->email->print_debugger();
}else{
//Email Failed To Send
echo $this->email->print_debugger();
}
回答by Tim Duncklee
After fighting with this same problem for a couple hours I finally decided to change my config to send through a different server. My original server for some reason would send to some addresses but not others (in same domain). As soon as I changed to sendgrid it worked as expected.
在与同样的问题斗争了几个小时后,我终于决定更改我的配置以通过不同的服务器发送。由于某种原因,我的原始服务器会发送到某些地址而不是其他地址(在同一域中)。一旦我更改为 sendgrid,它就会按预期工作。
If you are not getting the results you expect, try a different smtp server. The problem may not be your code...
如果您没有得到预期的结果,请尝试不同的 smtp 服务器。问题可能不是您的代码...
回答by Niroshan
I had the same problem and I try below code instead of Codeignitor's mail function.
我遇到了同样的问题,我尝试使用下面的代码而不是 Codeignitor 的邮件功能。
mail('[email protected]' , 'Test', 'Test Email');
It works and mail is send to the email address. That mail has sent by already created email address (As i think). In my case it is:
它可以工作并且邮件被发送到电子邮件地址。该邮件已通过已创建的电子邮件地址发送(我认为)。就我而言,它是:
[email protected]
So I copy this email address and try it with below code.
所以我复制这个电子邮件地址并使用以下代码尝试。
$this->email->from('[email protected]', 'www.domainserver.com');
And it work fine. It seems to be some servers need already created email address to send the email while others are NOT.
它工作正常。似乎有些服务器需要已经创建的电子邮件地址来发送电子邮件,而其他服务器则不需要。
Hope this is clear and helpful.
希望这是清楚和有帮助的。