Как отправить письмо на PHP с вложением, полученным по запросу POST? - PullRequest
0 голосов
/ 25 апреля 2019

Я загружаю изображение из приложения Android в почтовую программу PHP, чтобы оно могло отправить его на нужный адрес электронной почты. Я не могу понять, что не так в моем коде.

Я использую POST-запрос для захвата вложения, адреса электронной почты получателя. Я использовал POSTMAN для отправки запроса. POSTMAN завершается успешно, и в журнале ошибок php ошибок нет.

   <?php 

require_once 'DB_Connect.php';
    // connecting to database

$db = new Db_Connect();
     $conn = $db->connect(); 


  if($_SERVER['REQUEST_METHOD']=='POST' && isset($_FILES['attachment'])) 
  { 

  $from_email         = 'sender@gmail.com'; //from mail, sender email 
 addrress  $to = "sender@gmail.com"; 
$recipient_email    =  $_POST['recipient_email']; //recipient email 
 addrress 
 //   $recepient_name    =  $_POST['username']; //recipient email addrress 

    $file = "logo.png";
//  
//Load POST data from HTML form 

$subject        = " You sent a new email!"; //subject for the email 
$message        = "Hi . \n\n You sent an attachment . Please find the image you sent below." ;//body of the email 



//Get uploaded file data using $_FILES array 
$tmp_name    = $_FILES['attachment']['tmp_name']; // get the temporary 
 file name of the file on the server 
$name        = $_FILES['attachment']['name'];  // get the name of the file 
  $size        = $_FILES['attachment']['size'];  // get size of the file 
  for 
 size validation 
 $type        = $_FILES['attachment']['type'];  // get type of the file 
$error       = $_FILES['attachment']['error']; // get the error (if any) 

 //validate form field for attaching the file 
 if($error > 0) 
{ 
    die('Upload error or No files uploaded'); 
} 

//read from the uploaded file & base64_encode content 
$handle = fopen($tmp_name, "rb");  // set the file handle only for reading 
 the file 
 $content = fread($handle, $size); // reading the file 
 fclose($handle);                  // close upon completion 

 $encoded_content = chunk_split(base64_encode($content)); 

$boundary = md5("random"); // define boundary with a md5 hashed value 

 //header

$headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version 
$headers .= "Reply-To: Sender<sender@gmail.com>\r\n"; 
$headers .= "Return-Path: Sender<sender@gmail.com>\r\n";
$headers .= "From: Sender<sender@gmail.com>\r\n"; 
$headers .= "Content-Type: multipart/mixed;\r\n"; // Defining Content-Type 
$headers .= "boundary = $boundary\r\n"; //Defining the Boundary 


//plain text  
$body = "--$boundary\r\n"; 
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n"; 
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";  
$body .= chunk_split(base64_encode($message));  

//attachment 
$body .= "--$boundary\r\n"; 
$body .="Content-Type: $type; name=".basename($name)."\r\n"; 
 $body .= "Content-Description: ".basename($name)."\r\n" ;
$body .="Content-Disposition: attachment\n ; 
filename=".basename($name)."\r\n";      
$body .="Content-Transfer-Encoding: base64\r\n"; 
$body .="X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n";  
$body .= $encoded_content; // Attaching the encoded file with email 

$sentMailResult = mail($recipient_email, $subject, $body, $headers); 



if ($sentMailResult){ 
$response['error'] = false;

    } else { 
      $response['error'] = true;
    }

     echo json_encode($response);

 }   

 ?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...