1

I'm having some problems with the action of an Ajax form.

Here is the Javascript:

<script type="text/javascript">
var $j = jQuery.noConflict(); 

$j(document).ready(function() {
    $j(document).on('submit', '#submitNew', function() {  
        $j.post('http://www.winetube.eu/engine.php', $j(this).serialize()).done(function(data) {
            $j("#submitNew").fadeOut('slow', function() {
                $j(".result").fadeIn('slow');
            });
        }).fail(function() {
            alert('fail sending request');
        });
        return false;
    }); 

}); 
</script>

When I hit submit on the form, everything's fine so far, but nothing gets sent to my email address.

This is the PHP for the form:

<?php
// Recupero il valore dei campi del form
$destinatario = 'test@test.com';
$name = $_POST['name'];
$email = $_POST['email'];
$country = $_POST['country']; 
$quantity = $_POST['quantity'];
$wine = $_POST['wine'];
$message = $_POST['message'];


$messaggio = "Richiesta di preventivo da: ".$name."
              E-mail: ".$email."
              Paese: ".$country."
              Quantità: ".$quantity."
              Vino: ".$wine."
              Richiesta: ".$message."";

// Valorizzo le variabili relative all'allegato
$allegato = $_FILES['allegato-visura']['tmp_name'];
$allegato_type = $_FILES['allegato-visura']['type'];
$allegato_name = $_FILES['allegato-visura']['name'];

// Creo 2 variabili che riempirò più avanti...
$headers = "From: " . $name;
$msg = "";

// Verifico se il file è stato caricato correttamente via HTTP
// In caso affermativo proseguo nel lavoro...
if (is_uploaded_file($allegato))
{
  // Apro e leggo il file allegato
  $file = fopen($allegato,'rb');
  $data = fread($file, filesize($allegato));
  fclose($file);

  // Adatto il file al formato MIME base64 usando base64_encode
  $data = chunk_split(base64_encode($data));

  // Genero il "separatore"
  // Serve per dividere, appunto, le varie parti del messaggio.
  // Nel nostro caso separerà la parte testuale dall'allegato
  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

  // Aggiungo le intestazioni necessarie per l'allegato
  $headers .= "\nMIME-Version: 1.0\n";
  $headers .= "Content-Type: multipart/mixed;\n";
  $headers .= " boundary=\"{$mime_boundary}\"";

  // Definisco il tipo di messaggio (MIME/multi-part)
  $msg .= "This is a multi-part message in MIME format.\n\n";

  // Metto il separatore
  $msg .= "--{$mime_boundary}\n";

  // Questa è la parte "testuale" del messaggio
  $msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
  $msg .= "Content-Transfer-Encoding: 7bit\n\n";
  $msg .= $messaggio . "\n\n";

  // Metto il separatore
  $msg .= "--{$mime_boundary}\n";

  // Aggiungo l'allegato al messaggio
  $msg .= "Content-Disposition: attachment;\n";
  $msg .= " filename=\"{$allegato_name}\"\n";
  $msg .= "Content-Transfer-Encoding: base64\n\n";
  $msg .= $data . "\n\n";

  // chiudo con il separatore
  $msg .= "--{$mime_boundary}--\n";
}
else
{
  $msg = $messaggio;
}

// Invio la mail
if (mail($destinatario, $name, $msg, $headers))
{
  echo $_SERVER['PHP_SELF'];
}else{
  echo "<p>Errore!</p>";
}
?>

I tried everything but with no success. What could I do?

Thank you very much for any help.

closed as unclear what you're asking by mickmackusa, TryHarder, Eoin, Rene Korss, Michael Yaeger May 31 at 22:31

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    If you're using Joomla, please make full use of the Joomla API. You shouldn't be using mail(), $_POST, $_SERVER and should be utilising com_ajax. Please also check your console log for any errors. – Lodder Jul 18 '16 at 9:05
  • The fact is that before working properly . Now a few days does not send the email . $ _POST I always used and I have never created problems ... Possible solutions?! – Gabriel91 Jul 18 '16 at 9:08
  • Did it stop working after the Joomla 3.6 update? – Lodder Jul 18 '16 at 9:11
  • After Joomla 3.5.1 update, exactly! – Gabriel91 Jul 18 '16 at 9:14
  • 1
    I would break down the script to just sending a simple mail to a predefined address to check first if the mailer class is working. Then try the same again but writing the post form data to the message in the mail to check if everything is ok with the delivered data. (Or just print_r with die the post and check in the dev tools the response (chrome can do that).) – Dennis Heiden Jul 27 '16 at 18:10

Browse other questions tagged or ask your own question.