【问题标题】:codeigniter email config with minimum setting is not working具有最低设置的 codeigniter 电子邮件配置不起作用
【发布时间】:2015-10-30 12:17:42
【问题描述】:

下面的代码适用于我的其他项目,但是当我将它上传到服务器时,它不起作用,任何人都可以帮我解决这个问题。

我为发送电子邮件创建了一个帮助文件

    $ci =& get_instance();
    $config = Array(
          'mailtype'  => 'html',
          'charset'   => 'iso-8859-1',
          'validate'  => TRUE ,
          'newline'   => "\r\n",
          'wordwrap'  => TRUE
    ); 

    $ci->load->library('email');
    $ci->email->initialize($config);

    $ci->email->from($fromemail, $fromname);
    $ci->email->reply_to(APPLICATION_EMAIL, SITENAME);
    $ci->email->to($toemail);

    $template_msg=str_replace($replace_array,$new_array,$tempmsg);

    $ci->email->subject(sprintf($subject, $data['site_name']));
    $ci->email->message($template_msg);
    $ci->email->send();  

【问题讨论】:

  • 错误信息是什么?您从哪里获得变量 $fromemail、$fromname、$tomail、$subject、$data['site_name']、$template_msg?
  • 我已经创建了通用函数,所以这个值来自函数调用。

标签: php codeigniter email


【解决方案1】:

首先创建一个自定义配置文件

应用程序/配置中的email.php

在我的情况下,我通过 webmail id 发送电子邮件,所以这是我的 email.php

$config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'SMTP_HOST_NAME',
    'smtp_port' => 25,
    'smtp_user' => 'SMTP_USER_NAME', // change it to yours
    'smtp_pass' => 'SMTP_PASSWORD', // change it to yours
    'mailtype' => 'html',
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE
);

然后确保这个配置是自动加载的。在 application/config 中打开你的 Autoload.php 并写入

$autoload['config'] = array('email');

现在,每当您使用电子邮件库创建具有许多方法的控制器时。使用父结构

function __construct()
{
  parent::__construct();          
  $this->load->library('email', $config);

}

然后你就可以轻松地发邮件了

$this->email->from('info@example.net', 'Account');
 $this->email->to('johndoe@example.com');
 $this->email->cc('johndoe@example.com');
 $this->email->bcc('johndoe@example.com');
 $this->email->subject('Account Confirmation');
 $message = "any message body you want to send";
 $this->email->message($message);
 $this->email->send();

这是通过 CI 电子邮件库发送邮件时的最佳选择。您可以获得更多信息。关于 CI 电子邮件here。 谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    • 2015-11-03
    • 2015-01-27
    • 2014-07-05
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多