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() {
        }).fail(function() {
            alert('fail sending request');
        return false;


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:

// 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));

  // 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";
  $msg = $messaggio;

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

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

Thank you very much for any help.

  • 1
    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
  • And what console log errors are you getting? Without knowing what the errors are, it's hard to give any proper answer – Lodder Jul 18 '16 at 9:42

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.