15

我不知道为什么如果我尝试使用 CI 电子邮件类它不发送电子邮件,而如果我使用原生 PHP 邮件()类可以工作。

必须注意,有时我收到“已发送电子邮件”,而实际上并未发送,有时我收到错误消息“我的服务器未设置”。

我试图弄清楚如何设置它,但......没有......

控制器代码如下:

 if($this->form_validation->run()){

                //Set Language
                $this->lang->load('site', $this->session->userdata('lang'));

                //Random key
                $user_valid_key = md5(uniqid());

                //Prepare email
                $this->load->library('email', array('mailtype' => 'html'));
                $this->email->from($this->config->item('email_signup_from'), 'Wondermark.net');
                $this->email->to($this->input->post('email'));
                $this->email->subject($this->lang->line('email_signup_subject'));

                //Format mail con %s per inserire i campi necessari
                $signup_msg = sprintf($this->lang->line('email_signup_message'), $this->input->post('fname'), base_url().'main/signup_confirm/'.$user_valid_key);

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

                if($this->email->send()){
                    //TODO: load view...
                    echo "email sent";
                }else{
                    $to = $this->input->post('email');
                    mail($to, 'test', 'Other sent option failed');
                    echo $this->input->post('email');
                    show_error($this->email->print_debugger());
                }

                //TODO: Add to db

            }else{

            // Form validation failed

}
4

7 回答 7

31

使用此设置电子邮件..

$this->load->library('email');

$config['protocol']    = 'smtp';
$config['smtp_host']    = 'ssl://smtp.gmail.com';
$config['smtp_port']    = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user']    = 'sender_mailid@gmail.com';
$config['smtp_pass']    = 'password';
$config['charset']    = 'utf-8';
$config['newline']    = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not      

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

$this->email->from('sender_mailid@gmail.com', 'sender_name');
$this->email->to('recipient@gmail.com'); 
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');  

$this->email->send();

echo $this->email->print_debugger();
于 2013-10-17T12:41:35.123 回答
11

我遇到了这个问题并找到了以下解决方案。只需对电子邮件配置进行一点更改,它就可以 100% 工作:

$config['protocol'] = 'ssmtp';
$config['smtp_host'] = 'ssl://ssmtp.gmail.com';
于 2015-04-30T10:06:30.500 回答
2

Codeigniter 用户指南:https ://www.codeigniter.com/user_guide/libraries/email.html

此设置对我有用:

$config = Array(
            'protocol' => 'smtp',
            'smtp_host' => 'Your SMTP Server',
            'smtp_port' => 25,
            'smtp_user' => 'Your SMTP User',
            'smtp_pass' => 'Your SMTP Pass',
            'mailtype'  => 'html'
            );
$this->load->library('email', $config);
$this->email->set_newline("\r\n");

//Add file directory if you need to attach a file
$this->email->attach($file_dir_name);

$this->email->from('Sending Email', 'Sending Name');
$this->email->to('Recieving Email address'); 

$this->email->subject('Email Subject');
$this->email->message('Email Message'); 

if($this->email->send()){
   //Success email Sent
   echo $this->email->print_debugger();
}else{
   //Email Failed To Send
   echo $this->email->print_debugger();
}
于 2013-08-01T15:04:07.113 回答
2

我用了很多时间在localhost中运行我的配置代码,但它总是给我一个错误(无法使用 PHP SMTP 发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。)

但是当在我的服务器中运行下面的代码时,它对我有用。

应用程序>控制器>发送email_Controller.php

public function send_mail() {
    $this->load->library('email');   
    $config = array();
    $config['protocol']     = "smtp"; // you can use 'mail' instead of 'sendmail or smtp'
    $config['smtp_host']    = "ssl://smtp.googlemail.com";// you can use 'smtp.googlemail.com' or 'smtp.gmail.com' instead of 'ssl://smtp.googlemail.com'
    $config['smtp_user']    = "my@gmail.com"; // client email gmail id
    $config['smtp_pass']    = "******"; // client password
    $config['smtp_port']    =  465;
    $config['smtp_crypto']  = 'ssl';
    $config['smtp_timeout'] = "";
    $config['mailtype']     = "html";
    $config['charset']      = "iso-8859-1";
    $config['newline']      = "\r\n";
    $config['wordwrap']     = TRUE;
    $config['validate']     = FALSE;
    $this->load->library('email', $config); // intializing email library, whitch is defiend in system

    $this->email->set_newline("\r\n"); // comuplsory line attechment because codeIgniter interacts with the SMTP server with regards to line break

    $from_email = $this->input->post('f_email'); // sender email, coming from my view page 
    $to_email = $this->input->post('email'); // reciever email, coming from my view page
    //Load email library

    $this->email->from($from_email);
    $this->email->to($to_email);
    $this->email->subject('Send Email Codeigniter'); 
    $this->email->message('The email send using codeigniter library');  // we can use html tag also beacause use $config['mailtype'] = 'HTML'
    //Send mail
    if($this->email->send()){
        $this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
        echo "email_sent";
    }
    else{
        echo "email_not_sent";
        echo $this->email->print_debugger();  // If any error come, its run
    }
}

我定义f_emailemail的视图页面来自 post 方法。 应用程序>查看>电子邮件测试.php

<html>
<head>    
    <title> Send Email Codeigniter </title>
</head>
<body>
<?php
echo $this->session->flashdata('email_sent');
echo form_open('/Sendingemail_Controller/send_mail');
?>
<input type = "email" name = "f_email" placeholder="sender email id (from)"  required />
<input type = "email" name = "email"  placeholder="reciever email id (to)" required />
<input type = "submit" value = "SEND MAIL">
<?php
echo form_close();
?>
</body>

如果再次出现错误,请访问以下官方文档:

https://codeigniter.com/user_guide/libraries/email.html

于 2020-02-13T12:44:13.010 回答
1

在与同样的问题斗争了几个小时后,我终于决定更改我的配置以通过不同的服务器发送。由于某种原因,我的原始服务器会发送到某些地址,但不会发送到其他地址(在同一域中)。一旦我更改为 sendgrid,它就会按预期工作。

如果您没有得到预期的结果,请尝试使用不同的 smtp 服务器。问题可能不是您的代码...

于 2014-01-20T22:48:10.313 回答
1

我遇到了同样的问题,我尝试使用下面的代码而不是 Codeignitor 的邮件功能。

mail('mypersonalmail@domainserver.com' , 'Test', 'Test Email');

它有效,邮件被发送到电子邮件地址。该邮件已通过已创建的电子邮件地址发送(我认为)。就我而言,它是:

gtt28651ff@p3plcpnl0552.srod.ahx3.domainserver.com

所以我复制了这个电子邮件地址并用下面的代码试试。

$this->email->from('gtt28651ff@p3plcpnl0552.srod.ahx3.domainserver.com', 'www.domainserver.com');

它工作正常。似乎有些服务器需要已经创建的电子邮件地址来发送电子邮件,而其他服务器则不需要。

希望这是清楚和有帮助的。

于 2016-01-08T06:09:18.427 回答
-2

我花了几个小时试图更改端口,在 php.ini 文件中启用 ssl 扩展名等,但没有任何效果,只以消息 SMTP 服务器未正确配置。我使用 WAMP 作为本地主机,当我关闭我的防病毒 avast相同的配置有效!已成功发送。因此可能是您的防病毒程序在本地计算机上被阻止。

于 2021-08-14T16:04:16.017 回答