0

我是 CI 的初学者。

这是我在welcome.php 中的代码

class Welcome extends CI_Controller {


public function index()
{
$this->load->helper('url');
    $this->load->view('welcome_message');
}
public function emailSend()
{
$this->load->library('upload');
$this->load->library('email');

$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->upload->initialize($config);







$this->email->from($this->input->post('from'), $this->input->post('name'));
$this->email->to('taryar.t1@gmail.com');
//$this->email->cc('another@another-example.com');
//$this->email->bcc('them@their-example.com');

$this->email->subject($this->input->post('subject'));
$this->email->message($this->input->post('body'));

if($this->upload->do_upload())
{
    $attachdata=$this->upload->data();
$this->email->attach($attachdata['full_path']);
}

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

    else
    {
        show_error($this->email->print_debugger());
    }

}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

此代码仅适用于文本,不适用于附件(图像)。错误是“无法使用 PHP mail() 发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。”

我该如何解决这个错误?帮帮我。

4

3 回答 3

1
        $config['charset'] = 'iso-8859-1';
        $config['wordwrap'] = TRUE;
        $config['mailtype'] = 'html';

        $this->email->initialize($config);
        $to = $this->input->post('to'); // email@gmail.com
        $cc = 'email@gmail.com'; // email@gmail.com
        $subject = $this->input->post('subject');
        $message = $this->input->post('message');

        $this->email->to($to);
        $this->email->cc($cc);
        $this->email->from('info@awandevelopers.com','Awan Developers');
        $this->email->subject($subject);
        $this->email->message($message);


        $config['upload_path'] = './attachments/'; // or you can use any folder like uploads
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768'; 
        $config['encrypt_name']     = true;
        $this->load->library('upload', $config);

        if(!$this->upload->do_upload('files')) // files will be input attachment field name
        {
            $this->upload->display_errors();
        }else{
            $image_data = $this->upload->data();
            $fname=$image_data['file_name'];
            $fpath=$image_data['file_path'].$fname;
            $this->email->attach($fpath);

            if ($this->email->send()){
                echo "Mail Sent!";
            }
            else{
                echo "There is error in sending mail!";
            }
        }   
于 2015-08-26T11:00:54.923 回答
0
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png|txt|php|pdf';
$config['max_size']         = '9000';
$config['encrypt_name']     = true;

$image_data = $this->upload->data();
$fname=$image_data['file_name'];
$fpath=$image_data['file_path'].$fname;

$this->email->attach($fpath);

上面的代码将解决您的问题。同样的问题也发生在我身上。这是因为您保存在文件夹中的文件名不同,因为您附加了上面的代码将解决它,因为它采用了您上传文件夹的正确路径。请注意,上传文件夹应位于根目录中。

于 2013-09-05T15:32:21.727 回答
0

从官方文档中尝试这个..

$this->email->attach('/path/to/photo1.jpg');
$this->email->send();
于 2013-09-05T10:43:32.267 回答