Portal belajar membuat aplikasi Augmented Reality dan Virtual Reality

Senin, 17 Februari 2020

Sent email from unity with image attachment

09.01 Posted by Dicky Rachmat Iskandar No comments
UNITY

using System.Collections;
using UnityEngine.Networking;
using UnityEngine;

public class EmailController: MonoBehaviour
{
    public string email = "reciever@mail.com";
    public string screenShotURL = "https://www.your-domain.com/upload.php";

    void Start()
    {
       //sending email when start
        StartCoroutine(UploadPNG());
    }

    IEnumerator UploadPNG()
    {
        // We should only read the screen after all rendering is complete
        yield return new WaitForEndOfFrame();

        // Create a texture the size of the screen, RGB24 format
        int width = Screen.width;
        int height = Screen.height;
        var tex = new Texture2D(width, height, TextureFormat.RGB24, false);

        // Read screen contents into the texture
        tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        tex.Apply();

        // Encode texture into PNG
        byte[] bytes = tex.EncodeToPNG();
        Destroy(tex);

        // Create a Web Form
        WWWForm form = new WWWForm();
        form.AddField("name", email);
        form.AddBinaryData("fileToUpload", bytes, "ScreenShot.png", "image/png");

        // Upload to a cgi script
        using (var w = UnityWebRequest.Post(screenShotURL, form))
        {
            yield return w.SendWebRequest();
            if (w.isNetworkError || w.isHttpError)
            {
                print(w.error);
            }
            else
            {
                print(w.uploadHandler);
            }
        }
    }
}




PHP

<?php
$target_dir = "uploads/";
$target_file = basename($_FILES["fileToUpload"]["name"]);
$nama = basename( $_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

$subject = "MAIL FROM UNITY";
$from = "sender@gmail.com";
$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n"
  ."Content-Type: multipart/mixed; boundary=\"1a2a3a\"";

$message .= "If you can see this MIME than your client doesn't accept MIME types!\r\n"
  ."--1a2a3a\r\n";

$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
  ."Content-Transfer-Encoding: 7bit\r\n\r\n"
  ."Hey my <b>good</b> friend here is a picture of you"
  ."<p><span style='color:red'><b>PHOTO</b></span></p> \r\n"
  ."--1a2a3a\r\n";
 
 
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        // echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}

if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

$file = file_get_contents($nama);

$message .= "Content-Type: image/png; name=\"$nama\"\r\n"
  ."Content-Transfer-Encoding: base64\r\n"
  ."Content-disposition: attachment; file=\"$nama\"\r\n"
  ."\r\n"
  .chunk_split(base64_encode($file))
  ."--1a2a3a--";

// Send email

$success = mail($_POST["name"],$subject,$message,$headers);
   if (!$success) {
  echo "Mail to " . $_POST["name"] . " failed .";
   }else {
  echo "Success : Mail was send to " . $_POST["name"] ;
   }
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

0 komentar:

Posting Komentar