【问题标题】:Code Igniter -> attach emailCodeigniter -> 附加电子邮件
【发布时间】:2010-03-05 00:09:27
【问题描述】:

email->attach 功能如何使用?

我不知道发生了什么,因为当我输入电子邮件的代码时->附加消息是空白的(邮件正文)并且没有附加。

如果我删除该代码行,一切都会恢复正常..

谢谢

我的控制器(sendmail.php)

<?php

 class Sendmail extends Controller {

      function __construct() {
           parent::Controller();
           $this->load->library('email');
           $this->load->helper('url');
           $this->load->helper('form');
           $this->load->library('validation');
      }

      function index() {

           $info = array (
                'nome'  => $this->input->post('nome'),
                'mail'  => $this->input->post('email'),
                'motivo'    => $this->input->post('motivo'),
                'mensagem'  => $this->input->post('mensagem'),
                'anexo' => $this->input->post('upload'),
           );

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

           $this->email->clear();
           $this->email->from($info['mail'], $info['nome']);
           $this->email->to('example@mai.com');
     /* $this->email->cc(''); # não é preciso */
           $this->email->subject($info['motivo']);
           $this->email->message($info['mensagem']);
           $this->email->attach($info['anexo']);

           if ($this->email->send() ) {
                echo 'sent';
           }

           else {
            $this->load->view('formulario');
    # show_error( $this->email->print_debugger() );
           }

      }

 }
?>

我的看法(formulario.php)

    <?php
    echo form_open_multipart('davidslv/index.php/sendmail');
?>
          <label for="nome">nome</label>
          <input type="text" name="nome" id="nome" required />

          <label for="email">email</label>
          <input type="text" name="email" id="email" required />

          <label for="assunto">assunto</label>
          <select name="motivo">
               <option value="motivo1">motivo1</option>
               <option value="motivo2">motivo2</option>
               <option value="motivo3">motivo3</option>
          </select>

          <p> <label for="mensagem">mensagem</label>
          <textarea name="mensagem" id="mensagem" rows="8" cols="30" required></textarea>
          </p>

          <label for="upload">documento</label>
          <input type="file" id="upload" name="upload" size="18"/>
          <input type="submit" id="enviar" name="enviar" value="Enviar!" />

     </form>

【问题讨论】:

  • 您需要澄清您到底要做什么,附上 JPG/PDF,文件是由用户上传的吗?它是否在 /home/bob/photo.jpg 之类的目录中?等等……需要更多细节,伙计……
  • 另外,如何发布您目前拥有的代码,以便我们了解您可能做错了什么。
  • 嗨,我已经编辑了我的帖子以更清晰。目的是任何用户都可以向我发送电子邮件,但不知何故正文(mensagem)是空白的并且没有附件

标签: php email codeigniter


【解决方案1】:

您不能将表单上传字段中的文件直接附加到电子邮件中。您只能从服务器将文件附加到您的电子邮件,因此您需要使用 CI 上传库将文件从表单上传:$this->upload->do_upload() 到您的服务器到某个目录。需要配置上传库,允许哪些文件类型等。如果上传成功,do_upload 函数会返回有关文件存储位置的大量数据。您可以使用数组中的“full_path”索引将此文件附加到电子邮件中。然后发送邮件。之后,您可以从服务器中删除该文件。以下是一些可能有帮助的代码片段。

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

if($_FILES['upload']['size'] > 0) { // upload is the name of the file field in the form

$aConfig['upload_path']      = '/someUploadDir/';
$aConfig['allowed_types']    = 'doc|docx|pdf|jpg|png';
$aConfig['max_size']     = '3000';
$aConfig['max_width']        = '1280';
$aConfig['max_height']       = '1024';

$this->upload->initialize($aConfig);

  if($this->upload->do_upload('upload'))
  {
    $ret = $this->upload->data();
  } else {
    ...
  }

  $pathToUploadedFile = $ret['full_path'];
  $this->email->attach($pathToUploadedFile);
  ...
  $this->email->send();
  ...
}
...

希望这会有所帮助...

【讨论】:

  • 谢谢理查德,你的解释非常好。非常感谢:)
【解决方案2】:

$this->email->attach()

使您能够发送附件。放 第一个中的文件路径/名称 范围。注意:使用文件路径,而不是 一个网址。对于多个附件使用 该功能多次。为了 示例:

$this->email->attach('/path/to/photo1.jpg');
$this->email->attach('/path/to/photo2.jpg');
$this->email->attach('/path/to/photo3.jpg');

$this->email->send();

Codeigniter Email Class

【讨论】:

  • 我已经阅读了文档,这很容易做到,但是当您希望用户向您发送邮件时,情况就不同了。
【解决方案3】:

这是绝对正确的代码,请尝试

$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);

【讨论】:

  • 嗨朋友上面的代码会解决你的问题。同样的问题也发生在我身上。这是因为你保存在文件夹中的文件名不同,因为你附加上面的代码会解决它,因为它取上传文件夹的正确路径..请注意上传文件夹应该在根目录中....
【解决方案4】:

第 1 步:您不能将表单上传字段中的文件直接附加到电子邮件中。您只能从服务器将文件附加到您的电子邮件中,因此您需要使用 CI 上传库从表单上传文件:

$this-&gt;upload-&gt;do_upload() 到你的服务器到某个目录。

第 2 步:

 $file=upload file;


 $file_path='uploaded directory on your server(eg:uploads/career)'.$file;

第 3 步:只包含

$this->email->attach($file_path);
$this->email->send();

【讨论】:

    【解决方案5】:

    这是一个较晚的更新,但它可能有用。
    说了两遍

    "您不能直接从表单的上传字段附加文件 发送到电子邮件”

    。但是,这在 Codeigniter 3.0 中运行良好

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

    (但是,如果有两个同名文件,则不会发送电子邮件并且不会显示错误)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 2017-09-06
      • 2018-08-06
      相关资源
      最近更新 更多