14

我正在尝试在 CodeIgniter 上设置 SMTP。一切正常,我在页面上收到成功消息,该电子邮件发送没有错误。但是,电子邮件未送达。

这是我使用的代码:

$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'my-email@gmail.com', 
'smtp_pass' => '***', 
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);

$this->email->from('my-email@gmail.com', 'Explendid Videos');
$this->email->to('my-email@gmail.com');
$this->email->reply_to('my-email@gmail.com', 'Explendid Videos');


$this->email->subject('Explendid Video - Contact form');

$message = "Contact form\n\n";
$message .= "Name: ". $_POST['name'] . "\n";
$message .= "Phone: ". $_POST['phone'] . "\n";
$message .= "Email: ". $_POST['email'] . "\n";

$this->email->message($message);

$this->email->send();

可能是什么原因,电子邮件实际上并没有送达。

4

7 回答 7

30

将其更改为以下内容:

$ci = get_instance();
$ci->load->library('email');
$config['protocol'] = "smtp";
$config['smtp_host'] = "ssl://smtp.gmail.com";
$config['smtp_port'] = "465";
$config['smtp_user'] = "blablabla@gmail.com"; 
$config['smtp_pass'] = "yourpassword";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";

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

$ci->email->from('blablabla@gmail.com', 'Blabla');
$list = array('xxx@gmail.com');
$ci->email->to($list);
$this->email->reply_to('my-email@gmail.com', 'Explendid Videos');
$ci->email->subject('This is an email test');
$ci->email->message('It is working. Great!');
$ci->email->send();
于 2013-06-24T11:34:42.643 回答
5

代替

$config['protocol'] = 'smtp';

$config['protocol'] = 'sendmail';
于 2015-05-29T21:24:11.533 回答
4

这是在 apache2 服务器 ci 2.1.4 上为我工作的:它非常简单:首先在您的 application/config 目录下创建一个名为 email.php 的文件,然后在其中键入以下代码~>

<?php
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'u'r gmail account';
$config['smtp_pass'] = 'password of u'r account';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
?>

然后在您的应用程序/控制器目录下创建一个名为 email.php 的文件,然后输入此代码~>

    <?php
    class Email extends CI_Controller
    {

    function send()
    {
    // Loads the email library
    $this->load->library('email');
    // FCPATH refers to the CodeIgniter install directory
    // Specifying a file to be attached with the email
    // if u wish attach a file uncomment the script bellow:
    //$file = FCPATH . 'yourfilename.txt';
    // Defines the email details
    $this->email->from('some@of.mailaddress', 'ur Name');
    $this->email->to('email@detiny.your');
    $this->email->subject('Email Test');
    $this->email->message('Testing the email class.');
    //also this script
    //$this->email->attach($file);
    // The email->send() statement will return a true or false
    // If true, the email will be sent
    if ($this->email->send()) {
    echo "you are luck!";
    } else {
    echo $this->email->print_debugger();
    }
    }

    }
    ?>
于 2013-11-14T04:59:34.820 回答
0

你检查过你的 php.ini 文件吗?试试看。如果没有,那么也许您也可以尝试 SPF。SPF 或发件人策略框架是一种新技术,可以轻松检测垃圾邮件。除非您手动将这些电子邮件标记为非垃圾邮件,否则 Gmail 会采用 SPF。不管怎样,如果您收到来自另一个地址的电子邮件,那么它们也一定已经到达了 Gmail。彻底检查您的垃圾邮件,因为即使高度怀疑垃圾邮件,Gmail 也不会丢弃电子邮件,而是将它们最终放在垃圾邮件文件夹中。

您可以设置一个 SPF,允许您的网络服务器发送电子邮件,这将导致 Gmail 接受您的网络服务器发送的电子邮件作为真实电子邮件。请参阅http://www.mydigitallife.info/how-to-set-up-and-create-sender-policy-framework-spf-domain-dns-txt-record-with-wizard/和 Microsoft 的向导。

于 2013-06-24T11:04:36.400 回答
0

您可以更改此脚本,以调试您的问题,

$this->email->send();

if($this->email->send())
{
    echo 'Your email was sent.';
}

else
{
    show_error($this->email->print_debugger());
}
于 2013-06-24T11:18:04.677 回答
0

使用以下代码

并且不要在谷歌中无法遵循两个安全设置。

1) https://www.google.com/settings/security/lesssecureapps << 开启

2) https://accounts.google.com/b/0/DisplayUnlockCaptcha << 点击继续

** 如果您启用了两步验证,请关闭它。

$config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.gmail.com',
        'smtp_port' => 465,
        'smtp_user' => 'dkumara85@gmail.com',    //email id
        'smtp_pass' => 'xxxxxxxxxxx',            // password
        'mailtype'  => 'html', 
        'charset'   => 'iso-8859-1'
    );
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");

    $this->email->from('dkumara85@gmail.com','my name');
    $this->email->to("dkumara85@gmail.com"); // email array
    $this->email->subject('email subject');   
    $this->email->message("my mail body");

    $result = $this->email->send();


    show_error($this->email->print_debugger());  // for debugging purpose :: remove this once it works...
于 2015-08-14T19:08:02.277 回答
0

我刚刚修改了 RobinCominotto 的代码,使其在 office365 中工作。

PS:当我把它放在控制器中并像这样调用这个函数时,我让它工作了。当我将此配置放在 email.php (配置文件)上时不再起作用:(

    $ci = get_instance();
    $ci->load->library('email');
    $config['protocol'] = "smtp";
    $config['smtp_host'] = "smtp.office365.com";
    $config['smtp_port'] = "587";
    $config['smtp_user'] = "<HERE COMES YOUR EMAIL>"; 
    $config['smtp_pass'] = "<HERE COMES THE PASSWORD OF EMAIL>";
    $config['charset'] = "utf-8";
    $config['mailtype'] = "html";
    $config['newline'] = "\r\n";

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

    $ci->email->from('<HERE COMES YOUR EMAIL>', 'Blabla');
    $list = array('<HERE COMES TO EMAIL>', '<HERE COMES TO EMAIL>');
    $ci->email->to($list);
    $this->email->reply_to('<HERE COMES YOUR EMAIL>', 'Explendid Videos');
    $ci->email->subject('This is an email test');
    $ci->email->message('It is working. Great!');
    $ci->email->send();
    print_r($ci->email->print_debugger());
于 2016-09-13T12:36:19.277 回答