如何在Linux localhost中使用Codeigniter发送电子邮件

时间:2018-10-01 04:35:30

标签: php codeigniter

我有以下email.php文件:

<?php
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
$config['newline'] = "rn";
?>

在控制器中,我有下一个:

$this->load->library('email');
$this->email->from('agro@agro.com', 'Agro');
$this->email->to($Nom_usu);
$this->email->subject('Confirmar registro');
$this->email->message('<a href="'.base_url().'Cregistro/confirm/'.$Nom_usu.'">Click Aqui</a>');
$this->email->send();

但是,当我尝试发送电子邮件时,会得到下一个答案:

string(992) "Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.
User-Agent: CodeIgniter
Date: Mon, 1 Oct 2018 00:33:26 -0400
From: "Agro" <agro@agro.com>
Return-Path: <agro@agro.com>
Reply-To: <agro@agro.com>
X-Sender: agro@agro.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <5bb1a396c22bb@agro.com>
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="B_ALT_5bb1a396c22cb"
=?UTF-8?Q?Confirmar=20registro?=
This is a multi-part message in MIME format.
Your email application may not support this format.

--B_ALT_5bb1a396c22cb
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Click Aqui


--B_ALT_5bb1a396c22cb
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

=3Ca href=3D=22http://localhost/WebAgro2/Cregistro/confirm/juan.rodriguez=
=40ideasfractal.com=22=3EClick Aqui=3C/a=3E

--B_ALT_5bb1a396c22cb--
"

我在很多帖子中都提到了类似的问题,但是似乎没有任何效果。我在Linux中使用本地主机。

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

有2种方法可以在本地主机上测试电子邮件,一种是mailcatcher,您需要在开发机器上安装超级简单的smtp服务器上的mailcatcher,另一种是mailtrap,我更喜欢mailtrap,无需安装任何东西,只需创建一个在mailtrap上的帐户,登录后就可以在localhost上测试电子邮件了,您可以从mailtrap中获取帐户设置

$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
$config['newline'] = "rn";

if(ENVIRONMENT == 'development'){

$dev_config = Array(
  'protocol' => 'smtp',
  'smtp_host' => 'smtp.mailtrap.io',
  'smtp_port' => 2525,
  'smtp_user' => 'smtp_user_from_your_mailtrap',
  'smtp_pass' => 'smtp_pass_from_your_mailtrap',
  'crlf' => "\r\n",
  'newline' => "\r\n"
);

$config = array_merge($config, $dev_config);

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

和愉快的本地电子邮件测试。

答案 1 :(得分:0)

确保在配置email.php文件中至少设置了这四个配置首选项

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