【问题标题】:codeigniter email with attachment带附件的codeigniter电子邮件
【发布时间】:2017-08-29 11:52:34
【问题描述】:

我的问题是:在 Codeigniter 3.1.5 中发送带有附件的电子邮件 我想发送一个表格,其中包含用户上传的姓名、电子邮件、电话和 pdf 文件作为电子邮件的附件,但我遇到了问题。请帮帮我

表格:

    <div>
        <label for="name" class="label">Name:</label>
        <input type="text" name="name">
    </div>

    <div>
        <label for="email" class="label">Email:</label>
        <input type="email" name="email">
    </div>

        <div>
            <label for="email" class="label">Phone:</label>
            <input type="text" name="phone">
        </div>

        <hr>
    <div>
        <label for="file">File: </label>
        <input type="file" name="file">
    </div>

        <hr>
    <button type="submit" name="submit" class="button is-danger" >Send Form</button>
    </div>

    <?php echo form_close(); ?>

这是表单返回的控制器

控制器:

公共函数发送(){

    $config = [
        'porotocol' => 'sendmail',
        'smtp_host' => 'data',
        'smtp_port' => 993,
        'smtp_user' => 'email',
        'smtp_pass' => 'password',
        'mailtype' => 'html',
        'charset' => 'utf-8',
        'wordwrap' => TRUE,
    ];

        $this->load->library('email', $config);
        $this->email->set_newline("\r\n");

    $data = [
        'name' => $this->input->post('name'),
        'email' => $this->input->post('email'),
        'phone' => $this->input->post('phone'),
    ];

        $this->email->from($data['email'], $data['name']);
        $this->email->to('test@test.com');
        $this->email->subject('form');

    $file_name= $this->input->post('file');
    $this->email->attach($file_name);

        $message = $this->load->view('forms/formformat', $data, TRUE);
        $this->email->message($message);

    if($this->email->send()){
        $this->session->set_flashdata("email_sent","ok");
        redirect('forms/mailform');
    } else {
        $this->session->set_flashdata("email_nosent","error");
        $data['content'] = 'forms/mailform';
        $this->load->view('master', $data);
    }

}

我也试过上传库,但问题没有解决。 请您帮我如何将带有附件的表格发送到电子邮件。 谢谢

【问题讨论】:

标签: php codeigniter


【解决方案1】:

这段代码为我解决了:

        $aConfig['upload_path']      = './uploads/';
        $aConfig['allowed_types']    = 'pdf';
        $aConfig['max_size']     = '3000';
     $config['encrypt_name']     = true;
        $this->load->library('upload', $aConfig);


    foreach ($_FILES as $key => $file)
    {
        if ($file['error'] == 0)
        {
            $this->email->attach($file['tmp_name'], '', $file['name']);
        }
    }

【讨论】:

    猜你喜欢
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 2020-01-12
    相关资源
    最近更新 更多