带文件附件的Codeigniter电子邮件无法正常工作

时间:2017-05-14 14:01:40

标签: javascript php ajax codeigniter

我有一个联系表单(contact form = cart),用户可以上传文件附件,一切正常,但没有附件发送到邮件..我得到"你没有选择文件上传。"

我的观点:



<?php 
$order_cart_form_attr = array('class' => 'ui equal width form', 'id' => 'order_cart_form', 'enctype' => 'multipart/form-data');
echo form_open_multipart('Kassa/send_cart', $order_cart_form_attr);
?>

<div class="fields">
<div class="field ui left icon input">
      <input type="text" class="required-group" name="order_firstname" id="order_firstname" placeholder="Имя">
      <i class="user icon"></i>
</div>

<div class="field ui left icon input">
      <input type="text" name="order_lastname" id="order_lastname" placeholder="Фамилия">
      <i class="user icon"></i>
</div>
</div>

<div class="fields">
<div class="field ui left icon input">
      <input type="text" class="required-group" name="order_email" id="order_email"  placeholder="Email">
      <i class="mail icon"></i>
</div>

<div class="field ui left icon input">
      <input type="text" class="required-group" name="order_phone" id="order_phone" placeholder="Телефон">
      <i class="phone icon"></i>
</div>
</div>




<br><br>
<div class="fields">

<div class="field ui left icon input">
      <input type="text" class="required-group" name="order_inn" id="order_inn"  placeholder="ИНН">
      <i class="suitcase icon"></i>
</div>

<div class="field ui left icon input">
      <input type="file" name="order_file" id="order_file" />
      <i class="cloud upload icon"></i>
</div>


</div>
<br><br>




 <div class="field">
    <textarea class="required-group" name="order_comment" id="order_comment" placeholder="Комментарий к заказу"></textarea>
</div>



<button id="order_cart_submit_btn" class="ui fluid vertical animated button" tabindex="0">
<div class="hidden content"> <i class="shop icon"></i> </div>
<div class="visible content">
    Послать заявку
</div>
</button>

<div id="contactFormResponseMSG"></div>

<?php echo form_close(); ?>
&#13;
&#13;
&#13;

控制器:

&#13;
&#13;
public function send_cart() {


$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'UTF-8';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html'; 
$this->email->initialize($config);


$this->form_validation->set_error_delimiters('<div class="error">', '</div>');



$order_firstname = $this->input->post('order_firstname', TRUE);
$order_lastname = $this->input->post('order_lastname', TRUE);
$order_email = $this->input->post('order_email', TRUE);
$order_phone = $this->input->post('order_phone', TRUE);
$order_comment = $this->input->post('order_comment', TRUE);
$order_inn = $this->input->post('order_inn', TRUE);
$order_file = $this->input->post('order_file', TRUE);


$order_CartTotal = $this->cart->format_number($this->cart->total());

foreach ($this->cart->contents() as $items): 
 $order_CartInfo = $this->input->post('order_NameDiametr', TRUE);
 $order_NameDiametr = $items['name'].'  |  '.$items['options']['diametr']. "<br>";
endforeach;




$config['upload_path']          = './uploads/';
$config['allowed_types']        = '*';

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

if ( ! $this->upload->do_upload('order_file')) {
                   $error = array('error' => $this->upload->display_errors());
                   print_r($error);
                   print_r($this->upload->data());
}
else {
                       $this->upload->do_upload('order_file');
}




$this->form_validation->set_rules('order_firstname', 'Order_lastname', 'required|trim');
$this->form_validation->set_rules('order_lastname', 'Order_lastname', 'trim');
$this->form_validation->set_rules('order_email', 'Order_email', 'valid_email|trim');
$this->form_validation->set_rules('order_phone', 'Order_phone', 'required|trim');

if ($this->form_validation->run() == FALSE) {
    print "<div class='ui negative message'>";
	print  validation_errors();
    print "</div>";
}
else {
  
$this->email->set_newline("\r\n");	  
$this->email->from('---', '---');
$this->email->to('---');

$this->email->subject('---');


$body = "Имя: ".$order_firstname . "<br>";
$body .= "Фамилия: ".$order_lastname . "<br>";
$body .= "Телефон: ".$order_phone . "<br>";
$body .= "ИНН: ".$order_inn . "<br>";
$body .= "File: ".$this->email->attach($order_file) . "<br>";
$body .= "Email: ".$order_email . "<br><br>";
$body .= "Комментарий к заказу: ".$order_comment. "<br><br>";


foreach ($this->cart->contents() as $items): 
 $body .= $items['name'].'  |  '.$items['options']['diametr']. "<br><br>";
endforeach;


$body .= "Итого: ".$order_CartTotal." руб";
$this->email->message($body);   

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

$this->email->send();


}
&#13;
&#13;
&#13;

它可能依赖于ajax调用中的某些内容吗?

&#13;
&#13;
$(document).on("click", "#order_cart_submit_btn", function (e) {
	
$("#order_cart_form").validate({

rules: {
        order_firstname: "required",
        order_lastname: {
         required: false
		 },
         order_email: {
         required: false,
         email: true
		 },
		 order_phone: {
         required: true,
         digits: true
		 },
		 order_comment: {
         required: true
		 }
},
messages: {
	     order_firstname: {
         required: "Ваше имя"
         },
         order_lastname: {
         required: "Ваша фамилия"
         },
         order_phone: {
         required: "Ваш номер",
         digits: "Ваш номер"
	    /* minlength: jQuery.validator.format("Minst 8 siffror."),
	     maxlength: jQuery.validator.format("Max 15 siffror.")*/
         },
         order_email: {
         required: "Ваш email",
	     email: "Ваш email"
         },
         order_comment: {
         //required: "Комментарий к заказу"
         required: ""
         },
		 /*order_message: {
         required: "Vänligen skriv ett meddelande"
         },*/
},
         errorClass: 'error-view',
         validClass: 'success-view',
         errorElement: 'span',
         debug: true,
         
         
	   
	   submitHandler: function(form) {
       e.preventDefault();
	  
	    $.ajax({
        url: "Kassa/send_cart",
	    type: "POST",
	    data: $('#order_cart_form').serialize(),
      cache: false,
	    
		beforeSend: function(data, xhr, status) {
	    console.log(data);	   
        },
	    
		success: function(data, xhr, status) {
		   if (data.status == 'fail') {
           console.log("FAIL ALERT");
$("#contactFormResponseMSG").html('<div class="ui negative message">Ваша заявка не была отправлена. <br> Попробуйте еще раз позже.</div>');
$('#contactUsForm').form('clear');
        }
   else {
$("#contactFormResponseMSG").html('<div id="order_cart_form_success_msg" class="ui positive message">Спасибо. <br> Ваша заявка была успешно отправлена. Мы свяжемся с Вами в ближайшее время.</div>');
$('#order_cart_form').form('clear');
}
}
});
return false;
form.submit();
}

});
			
});
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

这是因为codeigniter 2中存在一个bug 新行无法在attache文件中设置,因此您必须在&#34;中编辑Email.php。包括/库/ Edit.php&#34; 转到第725行并更改

'content'   => chunk_split(base64_encode($file_content)),


'content'   => "\r\n" . chunk_split(base64_encode($file_content)),


第二个
请在发送之前添加此项以调试确切的错误

$this->email->print_debugger();

答案 1 :(得分:0)

<form action="---/Kassa/send_cart" class="ui equal width form" id="order_cart_form" enctype="multipart/form-data" method="post" accept-charset="utf-8">



<div class="fields">
<div class="field ui left icon input">
      <input type="text" class="required-group" name="order_firstname" id="order_firstname" placeholder="Имя" value="a">
      <i class="user icon"></i>
</div>

<div class="field ui left icon input">
      <input type="text" name="order_lastname" id="order_lastname" placeholder="Фамилия" value="b">
      <i class="user icon"></i>
</div>
</div>

<div class="fields">
<div class="field ui left icon input">
      <input type="text" class="required-group" name="order_email" id="order_email" placeholder="Email" value="aaaa@gmail.com">
      <i class="mail icon"></i>
</div>

<div class="field ui left icon input">
      <input type="text" class="required-group" name="order_phone" id="order_phone" placeholder="Телефон" value="07000">
      <i class="phone icon"></i>
</div>
</div>




<br><br>
<div class="fields">

<div class="field ui left icon input">
      <input type="text" class="required-group" name="order_inn" id="order_inn" placeholder="ИНН" value="111111">
      <i class="suitcase icon"></i>
</div>

<div class="field ui left icon input">
      <input type="file" name="order_file" id="order_file">
      <i class="cloud upload icon"></i>
</div>


</div>
<br><br>




 <div class="field">
    <textarea class="required-group" name="order_comment" id="order_comment" placeholder="Комментарий к заказу">cccc</textarea>
</div>



<button id="order_cart_submit_btn" class="ui fluid vertical  button" tabindex="0">
<div class="visible content">
    Послать заявку
</div>
</button>




<div id="contactFormResponseMSG"></div>


</form>