0

I use phpmailer to send mail on my form, the mailer.php send perfectly all email and work perfectly. I would like to know how to resize image, when the form in submitted. To have a fast upload for the user, is possible? If I create a separate script how can I integrate it with my script? Not having much experience where could I start?

<?php

    $nome        = $_POST["nome-locale"];
    $email       = $_POST["email"];
    $telefono    = $_POST["telefono"];
    $indirizzo   = $_POST["indirizzo"];
    $civico      = $_POST["civico"];
    $citta       = $_POST["citta"];
    $provincia   = $_POST["provincia"];
    $cap         = $_POST["cap"];
    $titolare    = $_POST["titolare"];
 $cf          = $_POST["codice_fiscale"]; 
    $declaration = isset($_POST["declaration"]) ? $_POST['declaration'] : 'No';
    $newsletter  = isset($_POST["newsletter"]) ? $_POST['newsletter'] : 'No';
    $data        = date('d-m-Y'); 

    $body = "<br>nome-locale:" . $nome . "<br>Email:" . $email . "<br>TelefonoLocale:" . $telefono . "<br>Indirizzo:" . $indirizzo . "<br>Civico:" . $civico . "<br>Città:" . $citta . "<br>Provincia:" . $provincia . "<br>Cap:" . $cap . "<br>Nome titolare:" . $titolare . "<br>CF:" . $cf . "<br>Declaration:" . $declaration . "<br>newsletter:" . $newsletter;


    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;



    require 'PHPMailer/src/Exception.php';
    require 'PHPMailer/src/PHPMailer.php';
    require 'PHPMailer/src/SMTP.php';
    // Instantiation and passing `true` enables exceptions
    if (isset($email) && $email !== '') 
    {
    $mail = new PHPMailer(true);

    try {
        //Server settings

        $mail->isSMTP(); // Send using SMTP
        $mail->Host       = 'smtps.aruba.it'; // Set the SMTP server to send through
        $mail->SMTPAuth   = true; // Enable SMTP authentication
        $mail->Username   = 'xx'; // SMTP username
        $mail->Password   = 'x'; // SMTP password
        $mail->SMTPSecure = 'ssl'; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
        $mail->Port       = x5; // TCP port to connect to

        //Recipients
        $mail->setFrom('x', $data);
        $mail->addAddress('x'); // Add a recipient


        // Attach the uploaded file
        if (isset($_FILES['menu1']['tmp_name'])) {
            $menu1_path = $_FILES['menu1']['tmp_name']; //actual path to the file
            $menu1_name = $_FILES['menu1']['name']; //actual name of the file
            $mail->AddAttachment($menu1_path, $menu1_name);
        }

        if (isset($_FILES['menu2']['tmp_name'])) {
            $menu2_path = $_FILES['menu2']['tmp_name']; //actual path to the file
            $menu2_name = $_FILES['menu2']['name']; //actual name of the file
            $mail->AddAttachment($menu2_path, $menu2_name);
        }

        // Content
        $mail->isHTML(true); // Set email format to HTML
        $mail->Subject = ('Iscrizione: ' . $nome);
        $mail->Body    = $body;
        $mail->AltBody = 'Iscrizione ricevuta da landing page';
  $mail->Send();
//
        $mail->ClearAllRecipients();
        $mail->clearAddresses();
        $mail->ClearAttachments();

     $mail->isHTML(true);
        $mail->Subject = ('Benvenuto, ' . $nome);
        $mail->setFrom('feed@vaimenu.it','Vaimenu.it');
        $mail->addAddress($email);

        $message = file_get_contents('Benvenuto.html');
        $message = str_replace('%Nome%', $nome, $message);
       
        $mail->MsgHtml($message);
   
        if ($mail->send()) {
            $response['success'] = true;
        } else {
            $response['error']   = true;
            $response['message'] = "Message could not be sent. Some thing went wrong";
        }
        echo json_encode($response);
        //    echo '<script>
        //    alert("Messaggio inviato correttamente");
        //    window.history.go(-1);
        //    </script>';
    }
    catch (Exception $e) {
        $response['error']   = true;
        $response['message'] = "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";

        echo json_encode($response);
    }
}
else
{
echo '<script>
alert("Inserisci una mail valida!");
window.history.go(-1);
</script>';
}
?>
<label class="control-label">Menu1</label>
<input type="file" name="menu1" accept=".jpg,.jpeg,.pdf,.png" required>
</div>
<button class="btn btn-success btn-lg fa-pull-right" type="submit">Finito!</button>
Synchro
  • 35,538
  • 15
  • 81
  • 104
  • 1
    This really isn't a PHPMailer question. PHPMailer will send whatever image file you give it, so if you want to send a resized uploaded file you need to deal with the upload (which your current code does not do safely - look at the [send file upload example](https://github.com/PHPMailer/PHPMailer/blob/master/examples/send_file_upload.phps)), resize the image, then give the resized image to PHPMailer. So your real question is simply ["how do I resize an image in PHP"](https://stackoverflow.com/search?q=how+do+I+resize+an+image+in+PHP), which has been answered many times before. – Synchro Feb 12 '20 at 10:48
  • "Where could I start" is a pretty broad question. What have you tried to solve that on your own? A hint: it might be easier to split up the problems. Solve your resizing questions first, and integrate that in your mailing stuff afterwards – Nico Haase Feb 12 '20 at 10:53
  • _“where could I start?”_ - with realizing that, if you want to speed up the upload, this has to happen on the client side to begin with, so that this is not even a PHP question then. https://stackoverflow.com/questions/23945494/use-html5-to-resize-an-image-before-upload – misorude Feb 12 '20 at 11:34

0 Answers0