CodeIgniter 上的 SMTP 显示成功,但电子邮件未发送到 Gmail 帐户

Posted

技术标签:

【中文标题】CodeIgniter 上的 SMTP 显示成功,但电子邮件未发送到 Gmail 帐户【英文标题】:SMTP on CodeIgniter shows success, but e-mail is not delivered to Gmail account 【发布时间】:2013-06-20 20:39:07 【问题描述】:

我正在尝试在 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();

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

【问题讨论】:

【参考方案1】:

您检查过您的 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 的向导。

【讨论】:

电子邮件没有进入垃圾邮件文件夹。【参考方案2】:

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

$this->email->send();

if($this->email->send())

    echo 'Your email was sent.';


else

    show_error($this->email->print_debugger());

【讨论】:

我尝试一切顺利,没有任何错误我应该做什么没有错误。如何找到这个。【参考方案3】:

将其更改为以下内容:

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

【讨论】:

谢谢!它通过将“ssl://smtp.googlemail.com”更改为“ssl://smtp.gmail.com” 这给了我fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known。从地址的开头删除ssl:// 有效。 我不明白问题 mail() 函数运行成功但没有收到电子邮件的原因。如果没有错误,我怎么知道问题?【参考方案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();
    
    

    
    ?>

【讨论】:

【参考方案5】:

替换

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

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

【讨论】:

请为您的答案添加一些解释,以使其对其他读者更有用。 成功了。尝试了许多其他解决方案几个小时。非常感谢【参考方案6】:

使用以下代码

并且不要因为无法遵循谷歌中的两个安全设置而感到沮丧。

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...

【讨论】:

【参考方案7】:

我刚刚修改了 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());

【讨论】:

以上是关于CodeIgniter 上的 SMTP 显示成功,但电子邮件未发送到 Gmail 帐户的主要内容,如果未能解决你的问题,请参考以下文章

使用 Amazon SES 的 Codeigniter SMTP 电子邮件

无法在 codeigniter 中从 gmail 发送电子邮件,SMTP 身份验证错误

使用 codeigniter 发送 html 邮件

使用 codeigniter 通过 gmail smtp 服务器发送电子邮件

用于发送邮件的 SMTP 协议不适用于 codeigniter

Gmail API 是不是支持没有 SMTP 的 Codeigniter?