【问题标题】:Sending email through contact form with file attachment通过带有文件附件的联系表发送电子邮件
【发布时间】:2013-11-05 03:48:42
【问题描述】:

我正在尝试使用 Codeigniter 创建一个联系表单,该表单将发送带有文件附件的电子邮件。现在发送电子邮件时,我得到的只是上传文件的名称,而不是实际的附件。有人可以帮帮我吗?

这是我正在使用的代码:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class sendmail_contact extends CI_Model {

function __construct()
{

    parent::__construct();

}

function send()
{

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

    $now = time();
    $contact_date = unix_to_human($now); // U.S. time, no seconds

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

    $name = $this->input->post('name', TRUE);
    $file = $this->input->post('file', TRUE);


    $this->email->from($email);
    $this->email->to('test@gmail.com'); 

    $this->email->subject('Subject');
    $this->email->message("Email" . $name . "\r\n" . $file); 

    $this->email->send();


}

【问题讨论】:

    标签: php codeigniter file-upload email-attachments


    【解决方案1】:

    $name 应该是文件的完整路径,您可以通过 echo 找到系统的根目录

    echo $this->config->item('server_root');
    

    你可以这样构建它:

    $this->config->item('server_root')."/path to needed folder/"
    

    你的文件的路径是:

    $name = $this->config->item('server_root')."/path to needed folder/"."file´s name";
    

    如果需要构建相对路径,可以在这里咨询如何做link

    【讨论】:

      【解决方案2】:

      首先,将附件保存到存储中。

      http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

      然后,在您保存用户上传的文件后,获取该文件的名称 并使用

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

      它会将文件附加到您要发送的电子邮件中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 2020-03-23
        • 1970-01-01
        • 2011-10-03
        • 2017-12-22
        • 2014-07-23
        相关资源
        最近更新 更多