2016-07-13 4 views
0

アップロードされたファイルをPHPを使用して電子メールで送信するのに苦労しています。私の問題は、電子メールでアップロードされたファイルにその電子メールに送信するものが何であれです。ここで私のコードは動作していません。アップロードされたファイルをPHPで電子メールに送信する方法

<?php 
     if (isset($_POST['submit'])) { 
     $headers = "From: [email protected] mailer \r\n"; 
     echo $message .= $_POST['message']; 
      /* GET File Variables */ 
     $tmpName = $_FILES['attachment']['tmp_name']; 
     $fileType = $_FILES['attachment']['type']; 
     $fileName = $_FILES['attachment']['name']; 

     if (file_exists($tmpName)) { 
      /* Reading file ('rb' = read binary) */ 

      $file = fopen($tmpName,'rb'); 
      $data = fread($file,filesize($tmpName)); 
      fclose($file); 

      /* a boundary string */ 
      $randomVal = md5(time()); 
      $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 

      /* Header for File Attachment */ 
      $headers .= "\nMIME-Version: 1.0\n"; 
      $headers .= "Content-Type: multipart/mixed;\n" ; 
      $headers .= " boundary=\"{$mimeBoundary}\""; 

      /* Multipart Boundary above message */ 
      $message = "This is a multi-part message in MIME format.\n\n" . 
      "--{$mimeBoundary}\n" . 
      "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
      "Content-Transfer-Encoding: 7bit\n\n" . 
      $message . "\n\n"; 

      /* Encoding file data */ 
      $data = chunk_split(base64_encode($data)); 

      /* Adding attchment-file to message*/ 
      $message .= "--{$mimeBoundary}\n" . 
      "Content-Type: {$fileType};\n" . 
      " name=\"{$fileName}\"\n" . 
      "Content-Transfer-Encoding: base64\n\n" . 
      $data . "\n\n" . 
      "--{$mimeBoundary}--\n"; 

     } 
     $to_user = "[email protected]"; 
     $flgchk = mail("$to_user", "$subject", "$message", "$headers"); 

      if ($flgchk) { 
      echo "Done, <br>"; 
      } 
      else 
      {   
      echo "Failed to send email, <br>"; 
      echo "Redirecting you to your <a href=\"?page=inbox.php\">Inbox</a>"; 
      } 
     } 
     ?> 
     <form name="form1" method="post" action="" enctype="multipart/form-data"> 
     <table> 
      <tr> 
      <td width='20'><b>Select File:</b> </td> 
      <td width='20'><input type="file" name="attachment"></td> 
      </tr> 
      <p><td> 
      <input type="submit" name="Submit" value="Send" class="Submit" /></td></tr></table> 
      </p> 
     </form> 

このコードを調整するか、私に別の作業コードを送ってください。

+0

は試してみて、自分のことをしないでください。混乱した合併症を避けるために、[SwiftMailer](http://swiftmailer.org/)のようなものを使用してください。 – Repox

答えて

1

PHPMailerを使用してデモを作成しました。最初にファイルphpmailer.class.phpをロードします。 あなたがここからダウンロードできます。http://github.com/PHPMailer/PHPMailer

require_once('/class.phpmailer.php'); // Your full path of phpmailer.php file 

$email = new PHPMailer(); 
$email->From  = 'From email address'; //From email address 
$email->FromName = 'Your name'; //your name 
$email->Subject = 'Message'; //Message 
$email->Body  = 'Body Text Message'; //Body message 
$email->AddAddress('To email address'); //To email address 

$attach_file = 'Your file path'; //Attached file path 

$email->AddAttachment($attach_file, 'file_name.doc'); 

return $email->Send(); 
+0

ローカルホストではないライブサーバーで作業しているため、phpmailerを使用しないでください – srikanth

+0

「添付ファイル付きメール送信」をお読みください。それはライブに取り組んでいます。 http://webcheatsheet.com/php/send_email_text_html_attachment.php –

+0

私のコードの上にこのコードを追加できますか? – srikanth

関連する問題