在codeigniter

时间:2015-11-19 06:18:11

标签: codeigniter email attachment

代码正在运行,但无法发送多个文件....只有图像可以发送到
控制器

    public function sende(){
            $from = $_POST['from'];
            $address = $_POST['address'];
            $to_name = $_POST['to_name'];
            $to_email = $_POST['to_email'];
            $cc = $_POST['cc'];
            $subject = $_POST['subject'];
            $mesg = $_POST['mesg'];
            $start = $_POST['start'];
            $end = $_POST['end'];
            $mid1 = $_POST['mid1'];
            $mid2 = $_POST['mid2'];
            $message = $start.$mid1.$mesg.$mid2.$end;
            $config = array(
                    'useragent' => 'CodeIgniter',
                    'protocol'  => 'smtp',
                    'smtp_host' => 'ssl://smtp.googlemail.com',
                    'smtp_port' => '465',
                    'smtp_user' => '***',
                    'smtp_pass' => '**',
                    'mailtype'  => 'html',
                    'charset'   => 'utf-8',
                    'newline'   => '\r\n'
                );

            $this->load->library('email',$config);
            $this->email->set_newline('\r\n');
            $this->email->from($address,$from);
            $this->email->to($to_email);
            $this->email->subject($subject);
            $this->email->message($message);
            $this->email->cc($cc);
            $path = $this->config->item('server_root');
            $file = $path.'/mshaadi/';
            $this->load->library('upload');
            $m = count($_FILES['atta']['name']);
            $files = $_FILES;
            for($i=0; $i< $m; $i++){
                $_FILES['atta']['name']= $files['atta']['name'][$i];
                $_FILES['atta']['type']= $files['atta']['type'][$i];
                $_FILES['atta']['tmp_name']= $files['atta']['tmp_name'][$i];
                $_FILES['atta']['error']= $files['atta']['error'][$i];
                $_FILES['atta']['size']= $files['atta']['size'][$i];
                $this->upload->initialize($this->set_upload_options());
                $this->upload->do_upload('atta');
                $a = $files['atta']['name'][$i];
                $mm = base_url().'images/'.$a;
                $this->email->attach($mm);
            }
            if($this->email->send()){
                redirect('Email');
            }else{
                show_error($this->email->print_debugger());
            }
}
private function set_upload_options(){
        $config = array();
        $config['upload_path'] = './images/';
        $config['allowed_types'] = 'gif|jpg|png|pdf|sql|docx|pptx';
        $config['max_size']      = '10000';
        $config['overwrite']     = TRUE;
        return $config;
    }

发送docx,pptx的多个附件时出错  220-gator3150.hostgator.com ESMTP Exim 4.85#2星期四,19十一月2015 00:10:43 -0600 220-我们不授权使用此系统来传输未经请求的,220和/或批量电子邮件。

220-gator3150.hostgator.com ESMTP Exim 4.85 #2 Thu, 19 Nov 2015 00:10:43 -0600 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail. 

hello: 250-gator3150.hostgator.com Hello 127.0.0.1 [112.196.141.163]
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-AUTH PLAIN LOGIN
250 HELP
from: 250 OK
to: 501 <>: missing or malformed local part
The following SMTP error was encountered: 501 <>: missing or malformed local part 
data: 503-All RCPT commands were rejected with this error:
503-501 <>: missing or malformed local part
503 Valid RCPT command must precede DATA
The following SMTP error was encountered: 503-All RCPT commands were rejected with this error: 503-501 <>: missing or malformed local part 503 Valid RCPT command must precede DATA 
500 unrecognized command 
The following SMTP error was encountered: 500 unrecognized command 
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
User-Agent: CodeIgniter
Date: Thu, 19 Nov 2015 07:10:37 +0100
From: "" <>
Return-Path: <>
Subject: =?ISO-8859-1?Q??=
Reply-To: "" <>
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <564d67dddeddc>
Mime-Version: 1.0


Content-Type: multipart/alternative; boundary="B_ALT_564d67dddeddc"

This is a multi-part message in MIME format.
Your email application may not support this format.

--B_ALT_564d67dddeddc
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit




--B_ALT_564d67dddeddc
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable



--B_ALT_564d67dddeddc--

2 个答案:

答案 0 :(得分:0)

对于多个附件,您可以多次使用相同的功能,在您的代码中,您还没有多次使用,也没有给出循环。

尝试使用完整路径而不仅仅是网址 -

 $this->email->attach('C:\Users\xyz\Desktop\images\abc.png');

$attched_file= $_SERVER["DOCUMENT_ROOT"]."/uploads/".$file_name;
$this->email->attach($attched_file);

也尝试更改charset as-

$config['charset'] = 'iso-8859-1';

Codeigniter send email with attach file

https://ellislab.com/codeigniter/user-guide/libraries/email.html

答案 1 :(得分:0)

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

//setting path to attach files 
$path = set_realpath('assets/your_folder/');
$file_names = directory_map($path);

    $this->email->clear(TRUE);
    $this->email->to($address);
    $this->email->from('your@example.com');
    $this->email->subject('Here is your info '.$name);
    $this->email->message('Hi  Here is the info you requested.'); 

    foreach($file_names as $file_name)
    {

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

    }

    $this->email->send();