【问题标题】:cant send email properly in codeigniter无法在 codeigniter 中正确发送电子邮件
【发布时间】:2014-02-20 15:44:04
【问题描述】:

我在使用 codeigniter 发送电子邮件时遇到问题。

$ci = get_instance();
$ci->load->library('email');
$config['protocol'] = "mail";
$config['smtp_host'] = "ssl://mail.smsgt.com";
$config['smtp_port'] = "25";
$config['smtp_user'] = "myemail@smsgt.com"; 
$config['smtp_pass'] = "";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";
$config['crlf'] = "\r\n";

这是我发送电子邮件时的代码

public function send_email_accountability($C11,$C12)
{

    date_default_timezone_set('Asia/Manila');
    $this->load->library('email');
    $this->email->set_mailtype("html");
    $this->email->from('noreply@smsgt.com', 'company email');
    $this->email->to($C11); 
    $this->email->subject('Accountability for'. " ". $C12);
    $this->email->message("testing");

    $check = $this->email->send();
    //echo $this->email->print_debugger(); 
    if ($check){
                $data = "true";
    }
            else{
                $data = "false";
            }

}

当我在 MESSAGE 中发送带有纯文本的电子邮件时,它工作正常。但问题是当我使用 HTML 脚本发送电子邮件时,它不会产生错误,但不会发送给用户,并且使用 MS OUTLOOK 也不会收到电子邮件。有人可以帮我解决这个问题吗?谢谢大家!

【问题讨论】:

  • 尝试使用 var_dump() 打印函数的参数

标签: php codeigniter email


【解决方案1】:

如果您可以提供echo $this->email->print_debugger(); 返回的可能错误,那将会有所帮助,那么为什么不现在启用它然后运行您当前的代码。

或者,试试这个:

public function send_email_accountability($C11,$C12)
{
  date_default_timezone_set('Asia/Manila');

  $ci =& get_instance();
  $ci->load->library('email');

  $config['protocol'] = "mail";
  $config['smtp_host'] = "ssl://mail.smsgt.com";
  $config['smtp_port'] = "25";
  $config['smtp_user'] = "myemail@smsgt.com"; 
  $config['smtp_pass'] = "";
  $config['charset'] = "utf-8";
  $config['mailtype'] = "html";
  $config['newline'] = "\r\n";
  $config['crlf'] = "\r\n";

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

  $ci->email->from('noreply@smsgt.com', 'company email');
  $ci->email->to($C11); 
  $ci->email->subject('Accountability for'. " ". $C12);
  $ci->email->message("testing");

  $check = $ci->email->send();

  //echo $ci->email->print_debugger(); 

  if ($check)
    $data = "true";
  else
    $data = "false";    
}

编辑

由于您在评论中提到echo $ci->email->print_debugger(); 返回“您的消息已使用以下协议成功发送”,这只是意味着您的脚本中没有语法错误。就像我说的,我的想法是邮件服务器问题。

如果我建议这将是我将如何调试您的问题:

  • 我将$C11 替换为$ci->email->to($C11); 中的Gmail 地址,然后运行我当前的脚本,看看延迟问题是否相同。
  • 将您当前的 SMTP 服务器凭据替换为 Mandrill 之类的内容,以确保其报告日志肯定会提示您正在发生的事情。

但不管怎样,我猜你最终还是会在你的邮件服务器 (@smsgt.com) 上挖掘一些东西。如果我是你,我会联系你的服务器管理员并开始在邮件服务器日志中寻找线索。

【讨论】:

  • 您的消息已使用以下协议成功发送:邮件,即我收到的消息。我的问题是现在用户接收速度很慢。这有什么细节吗?
  • $ci =& get_instance();是否有获取实例的 & 符号?
  • 是的,那是&。我的想法是邮件服务器问题。您是否尝试通过 @smsgt.com 以外的其他电子邮件发送?
  • 我需要将所有电子邮件发送到@smsgt.com,因为这是默认电子邮件。 smtp 端口是 25。
  • =& 是一个通过引用传递变量,关于$ci =& get_instance(); Anthony 很好地解释了这一点here。我确实建议将它发送到 @smsgt.com 以外的地方,以便您评估它是否真的是您的邮件服务器(传入)或(传出)的原因。
【解决方案2】:

尝试添加这个:

$this->email->priority(3);

您可以在这里找到更多信息:http://ellislab.com/codeigniter/user-guide/libraries/email.html

【讨论】:

  • $this->email->priority 的目的是什么?谢谢
  • 在某些延迟到达邮件服务器时使用优先级来加速邮件的接收
【解决方案3】:

您尚未使用您设置的配置值初始化您的电子邮件库。

您只需在代码中添加以下行。

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

【讨论】:

  • 我忘记包含 $this->email->initialize($config);我的问题是现在用户接收速度很慢。这有什么细节吗?
  • @vincent 你能简单解释一下你的问题吗?
  • @Shenal S 我的问题是,当我在发送中包含图像时。电子邮件根本不会发送。但是当我没有包括电子邮件时。我的电子邮件将被发送。并且通常需要2-5分钟才能发送
  • @vincent 你所需要的只是将协议配置为“邮件”,邮件类型为“html”
【解决方案4】:
public function send_email_accountability($C11,$C12)
{

    date_default_timezone_set('Asia/Manila');
    $this->load->library('email');
    $config['protocol'] = 'sendmail';
    $config['mailpath'] = '/usr/sbin/sendmail';
    $config['charset'] = 'utf-8';
    $config['wordwrap'] = TRUE;
    $config['mailtype'] = 'html';
    $this->email->initialize($config);
    //$this->email->set_mailtype("html");
    $this->email->from('noreply@smsgt.com', 'company email');
    $this->email->to($C11); 
    $this->email->subject('Accountability for'. " ". $C12);
    $this->email->message("testing");

    $check = $this->email->send();
    //echo $this->email->print_debugger(); 
    if ($check){
                $data = "true";
    }
            else{
                $data = "false";
            }

}

【讨论】:

    【解决方案5】:

    使用类似的东西

        $this->load->library('email', array(
                        'protocol' => 'mail',
                        'smtp_host' => 'ssl://mail.smsgt.com',
                        'smtp_port' => '25',
                        'smtp_user' => 'myemail@smsgt.com',
                        'smtp_pass' => '',
                        'newline' => '\r\n',
                        'crlf' => '\r\n',
                        'mailtype' => 'html'
            ));
    
        $this->email->to($C11);
        $this->email->from('noreply@smsgt.com', 'company email');
        $this->email->subject('Accountability for'. " ". $C12);
        $this->email->message("testing");
    
        if( $this->email->send() ) {
            return TRUE;
        } else {
            return FALSE;
        }
    

    【讨论】:

    • 我的问题是现在用户接收速度很慢。这有什么细节吗?
    【解决方案6】:

    试试这个代码,这是我用来发送电子邮件的代码

        function emailformat(){
                $config['protocol'] = 'smtp'; // mail, sendmail, or smtp    The mail sending protocol.
                $config['smtp_host'] = 'smtp.gmail.com'; // SMTP Server Address.
                $config['smtp_user'] = 'test@yahoo.com.ph'; // SMTP Username.
                $config['smtp_pass'] = '@password'; // SMTP Password.
                $config['smtp_port'] = '25'; // SMTP Port.
                $config['smtp_timeout'] = '5'; // SMTP Timeout (in seconds).
                $config['wordwrap'] = TRUE; // TRUE or FALSE (boolean)    Enable word-wrap.
                $config['wrapchars'] = 76; // Character count to wrap at.
                $config['mailtype'] = 'html'; // text or html Type of mail. If you send HTML email you must send it as a complete web page. Make sure you don't have any relative links or relative image paths otherwise they will not work.
                $config['charset'] = 'utf-8'; // Character set (utf-8, iso-8859-1, etc.).
                $config['validate'] = FALSE; // TRUE or FALSE (boolean)    Whether to validate the email address.
                $config['priority'] = 3; // 1, 2, 3, 4, 5    Email Priority. 1 = highest. 5 = lowest. 3 = normal.
                $config['crlf'] = "\r\n"; // "\r\n" or "\n" or "\r" Newline character. (Use "\r\n" to comply with RFC 822).
                $config['newline'] = "\r\n"; // "\r\n" or "\n" or "\r"    Newline character. (Use "\r\n" to comply with RFC 822).
                $config['bcc_batch_mode'] = FALSE; // TRUE or FALSE (boolean)    Enable BCC Batch Mode.
                $config['bcc_batch_size'] = 200; // Number of emails in each BCC batch.
    
                    $this->load->library('email');
                    $this->email->initialize($config);
                    $this->email->from('test@yahoo.com.ph', 'Robot');
                    $this->email->to('test@yahoo.com.ph');
                    $this->email->subject('Expiration Notification');
                    $this->email->message('<html><body>This Message is to notify!</body></html>');
                    $this->email->send();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      • 1970-01-01
      • 2013-06-15
      相关资源
      最近更新 更多