php - Codeigniter -> 附上电子邮件

标签 php email codeigniter

如何使用邮件->附件功能?

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

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

谢谢

我的 Controller (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>

最佳答案

您不能直接将表单上传字段中的文件附加到电子邮件中。您只能从您的服务器将文件附加到您的电子邮件,因此您需要使用 CIs 上传库将文件从表单上传:$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();
  ...
}
...

希望这对您有所帮助...

关于php - Codeigniter -> 附上电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2383672/

相关文章:

php - "insert duplicate"执行脚本的两张票

php - 使用 GitHub PHP Composer 和 Bash 自动部署

email - 使用 alfresco 作为 smtp 邮件服务器

html - 设置主题包含与号 (&) 的 mailto 链接

python - 使用python邮箱读取mbox文件的邮件内容

javascript - 如何使用codeigniter在MYSQL数据库中使用数组索引上传文件

php - 不唯一的表/别名 : 'request

PHP PDO 使用同一变量执行多个语句

php - 结论 : remove guzzlehttp/guzzle 6. 2.0

php - CodeIgniter 的 MySQL 表问题