无法使用 PHP SMTP 发送电子邮件。您的服务器可能未配置为使用此方法发送邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45252547/
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
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method
提问by Anas Ummalil
Iam using codeigniter
我正在使用 codeigniter
I exicuted the code on live server. got the following error using print_debugger()
我在实时服务器上执行了代码。使用 print_debugger() 得到以下错误
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
无法使用 PHP SMTP 发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。
public function sendEnquiry() {
$this->load->library('email');
$name = $this->input->post("fname");
$cemail = $this->input->post("email");
$pno = $this->input->post("phone");
$message = $this->input->post("message");
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://mail.gatewaykhobar.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = '***********';
$config['smtp_pass'] = '***********';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = FALSE;
$this->email->initialize($config);
$this->email->from('[email protected]','Gateway Restaurent Contact');
$this->email->to($cemail);
$this->email->subject('Gateway Restaurent Contact Enquiry');
$this->email->message($message);
$send = $this->email->send();
if($send) {
echo json_encode("send");
} else {
$error = $this->email->print_debugger(array('headers'));
echo json_encode($error);
}
}
回答by katwekibs
Change smtp_port from 465 to 587 Make sure $config['newline'] = "\r\n"; is in double quotes not single quotes
将 smtp_port 从 465 更改为 587 确保 $config['newline'] = "\r\n"; 是双引号不是单引号
回答by Tadeu RT
$mail_config['smtp_host'] = 'smtp.gmail.com';
$mail_config['smtp_port'] = '587';
$mail_config['smtp_user'] = '[email protected]';
$mail_config['_smtp_auth'] = TRUE;
$mail_config['smtp_pass'] = 'password';
$mail_config['smtp_crypto'] = 'tls';
$mail_config['protocol'] = 'smtp';
$mail_config['mailtype'] = 'html';
$mail_config['send_multipart'] = FALSE;
$mail_config['charset'] = 'utf-8';
$mail_config['wordwrap'] = TRUE;
$this->email->initialize($mail_config);
$this->email->set_newline("\r\n");
I just added the last line
我刚刚添加了最后一行
回答by pbarney
A common cause of this is the way that CodeIgniter interacts with the SMTP server with regards to line breaks. Your SMTP server might require \r\n
and CodeIgniter is using \n
.
造成这种情况的一个常见原因是 CodeIgniter 与 SMTP 服务器就换行符进行交互的方式。您的 SMTP 服务器可能需要\r\n
并且 CodeIgniter 正在使用\n
.
There is an easy fix: after your $this->email->initialize()
, add the following:
有一个简单的解决方法:在您的 之后$this->email->initialize()
,添加以下内容:
$this->email->set_newline("\r\n");
That should get it working for you.
那应该让它为你工作。
回答by Christian
Just use "mail" for the 'protocol' array item, and that's all...
只需将“邮件”用于“协议”数组项,仅此而已……
$config = array();
$config['useragent'] = $system_name;
$config['mailpath'] = "/usr/bin/sendmail"; // or "/usr/sbin/sendmail"
$config['protocol'] = "mail"; //use 'mail' instead of 'sendmail or smtp'
$config['smtp_host'] = "your domain name";
$config['smtp_user'] = $from;
$config['smtp_pass'] = "*************";
$config['smtp_port'] = 465;
$config['smtp_crypto'] = 'ssl';
$config['smtp_timeout'] = "";
$config['mailtype'] = "html";
$config['charset'] = "utf-8";
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$config['validate'] = FALSE;
回答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 Grzegorz
It looks like the mail server is hosted by yourself as well, try sending email from any e-mail client. If it fails - there's a problem with your mailserver config, not the code you pasted - check the server logs.
看起来邮件服务器也是由您自己托管的,请尝试从任何电子邮件客户端发送电子邮件。如果失败 - 您的邮件服务器配置有问题,而不是您粘贴的代码 - 检查服务器日志。