从Unity Ios应用程序发送到服务器的Pdf文件无效。(在android和unity Editor上运行)

时间:2018-09-13 10:03:03

标签: php ios unity3d server

我们正在使用Android,iOS和编辑器等平台上的统一应用程序将包含电子邮件ID和主题(字符串格式)的pdf文件发送到服务器。

以下php脚本正在服务器上运行,该服务器正在发送邮件和我们从统一应用程序发送的pdf文件。

无论何时我们通过简单的http服务器发送邮件附件,都会从Android,ios和编辑器正确发送。

但是使用https-server或带有SMTP协议的http-serrver-with-protocol时,pdf附件已损坏,并且仅在从ios设备发送时显示无效格式。

使用android和Unity编辑器发送时会收到正确的pdf附件,但使用ios设备发送时会显示pdf格式无效。

如果我们在任何地方出错,请更正我们,并提供正确的解决方案。

以下是php和c#脚本中使用的代码段。

   /************* Normal PHP mail Function *************/

 <?php 
//This attaches the file
$semi_rand     = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers      .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

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

$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .

"-{$mime_boundary}-\n";
// Send the email

if(mail($to, $subject, $message, $headers)) {

  echo "The email was sent.";
}

 else {

echo "There was an error sending the mail.";

}

?>

/ ********************* SMTP邮件*********************** **** /

 <?php 
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);          // Passing `true`  enables exceptions

 try {
 //Server settings
 //$mail->SMTPDebug = 2;             // Enable verbose  debug output

 $mail->isSMTP();                    // Set mailer to use SMTP
 $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
 $mail->SMTPAuth = true;             // Enable SMTP authentication
 $mail->Username = <username>;         // SMTP username
 $mail->Password = <password>;            // SMTP password
 $mail->SMTPSecure = 'tls';   // Enable TLS encryption, `ssl` also accepted
 $mail->Port = 587;                          // TCP port to connect to
 $mail->SMTPOptions = array(
'ssl' => array(
    'verify_peer' => false,
    'verify_peer_name' => false,
    'allow_self_signed' => true
                ));
//Recipients
$mail->setFrom('from',$name);
$mail->addAddress($email,$name);     // Add a recipient

  $mail->addAttachment($_FILES['file']['tmp_name'],$_FILES['file']['name']);         
 // Add attachments

 //Content
 $mail->isHTML(true);                          // Set email format to HTML
 $mail->Subject = $subject;
 $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
 $mail->AltBody = 'This is the body in plain text for non-HTML mail 
 clients';
 $mail->MsgHTML($mainMessage);                            // Message body
 $mail->send();
 echo json_encode(array('status'=>'1','msg'=>'Message has been sent'));
} catch (Exception $e) {

 echo json_encode(array('status'=>'0','msg'=>'Message could not be sent. 
Mailer Error: ', $mail->ErrorInfo));

 }

 ?>

/ ******************************在Unity c#************* ********************** /

public InputField EmailID;

string URL = "Link_Of_Above_Script/api-phpmail.php";

IEnumerator DownloadPDF(string url)
 {
    String myEmail = EmailID.text;
        WWW www = new WWW(url);
    yield return www;
    StartCoroutine(SendPDF(URL, myEmail, www.bytes));
}

IEnumerator SendPDF(string url,string name,byte[] b)
{
    //Content-Type
    //multipart/form-data
    WWWForm form = new WWWForm();
    form.AddField("email", name);
            form.AddField("subject", "Subject Of the mail");
             form.AddBinaryData("file", b,FileName, "application/octet- 
 stream");
         byte[] rawData = form.data;
    Dictionary<string, string> headers = form.headers;
    form.headers["Content-Type"] = "multipart/form-data";
    WWW www = new WWW(url,rawData,headers);
    yield return www;
    Debug.Log(www.text);
}

0 个答案:

没有答案