代码点火器 ->附上电子邮件 [英] Code Igniter -> attach email

查看:29
本文介绍了代码点火器 ->附上电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何使用电子邮件->附加功能?

How do you use the email->attach function?

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

I can't figure what is happen, cos when i put the code for email->attach the mesage came in blank(the mail body) and there is no attach.

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

If i remove that code line, everything come back to normal..

谢谢

我的控制器 (sendmail.php)

my 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('
');

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

my view (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>

推荐答案

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

You can not directly attach a file from the upload field of your form to an email. You can only attach files to your email from your server, so you need to upload the file from the form with CIs upload library: $this->upload->do_upload() to your server into some directory. the upload library needs to be configured, which file types are allowed etc. if the upload was successful, the do_upload function returns extensive data about where the file is stored. you can use the 'full_path' index from the array to attach this file to the email. then send the mail. after that you may delete the file from your server. Here are some code fragments that might help.

$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();
  ...
}
...

希望这有帮助...

这篇关于代码点火器 ->附上电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆