简体   繁体   中英

jquery .post send email using swiftmailer

I am trying to send an email using swiftmailer(for attachments) and I have been using in the past the following jquery post code:

$.post("contact.php", $("#contact").serialize());

this use to work fine, but now for some reason it stopped working when making the switch to swiftmailer..If I have a page with action="contact.php" then it works but using the jquery post function seems not to..

here's the complete jquery code:

$(document).ready(function(){
$("#contact").submit(function(event) {
    var $form = $( this );
    var nombre = $form.find( 'input[name="name"]' ).val();
    var apellido = $form.find( 'input[name="apellido"]' ).val();
    var phone = $form.find( 'input[name="telefono"]' ).val();
    var email = $form.find( 'input[name="email"]' ).val();
    var jobtitle = $form.find( 'input[name="jobtitle"]' ).text();
    //var filesize = $form.find('input[name="file"]').files[0].size;
    if(nombre == "")
    {   
        $("#result").empty().append("Please fill in required fields.");
        $("#result").css('color','Red');                
        $('#name').focus();
        return false;
    }
    if(apellido == "")
    {   
        $("#result").empty().append("Please fill in required fields.");
        $("#result").css('color','Red');                
        $('#apellido').focus();
        return false;
    }
    if(email == "")
    {   
        $("#result").empty().append("Please fill in required fields.");
        $("#result").css('color','Red');                
        $('#email').focus();
        return false;
    }
    else
    {
        if( !isValidEmailAddress(email) ) { 
        $("#result").empty().append("invalid email.");
        $("#result").css('color','Red');                
        $('#email').focus();
        return false;
        }
    }
    if(phone == "")
    {   
        $("#result").empty().append("Please fill in required fields.");
        $("#result").css('color','Red');                
        $('#phone').focus();
        return false;
    }
    //if(filesize <= 0)
    //{
    //  $("#result").empty().append("Please attach your resume.");
       //   $("#result").css('color','Red');                
      //    $('#file').focus();
     // return false;
    //}
    /* stop form from submitting normally */
   //event.preventDefault(); 
   $.post("contact.php", $("#contact").serialize());
   $("#contact").attr('style', 'display:none');
   $("#result").empty()
   $("#success").empty().append("<h2>Thank you, we will review your information and contact you as soon as possible.</h2>");
   $("#success").css('color','#000');
   $("#success").attr('style', 'display:block');
   return false;
});

});

and my php file "contact.php" has this:

require_once 'lib/swift_required.php';

$transport = Swift_MailTransport::newInstance();

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

$nombre = $_POST['name'];
$apellido = $_POST['apellido'];
$email = $_POST['email'];
$telefono = $_POST['telefono'];
$title = $_POST['jobtitle'];

// Create the message
$message = Swift_Message::newInstance()

// Give the message a subject
->setSubject('Nuevo applicante para: ' . $title)

// Set the From address with an associative array
->setFrom(array('no-reply@email.com' => 'no-reply'))

// Set the To addresses with an associative array
->setTo('destination@email.com')

// Give it a body
->setBody('<html>
    <head></head>
    <body>
    <table>
    <tr>
    <td>Nombre:</td><td>' . $nombre . ' ' . $apellido .'</td>
    </tr>
    <tr>
    <td>Email:</td><td>' . $email . '</td>
    </tr>
    <tr>
    <td>Telefono:</td><td>'. $telefono .'</td>
    </tr>
    </table>
</body>
</html>', 'text/html')

 // Optionally add any attachments
 ->attach(Swift_Attachment::fromPath($_FILES["file"]["tmp_name"])
 ->setFilename($_FILES['file']['name']));

// Send the message
$result = $mailer->send($message);

?>

Here are test links:

with jquery post: here (click on "Apply" and a popup should appear)

just php: here

Stop using MailTransport . MailTransport just calls mail() under the covers.

mail() is notoriously unreliable because most hosting providers don't bother configuring their servers sanely.

Use SendmailTransport or SmtpTransport instead. Your host surely has an SMTP server or a Sendmail installation. Check their documentation, they should the sendmail path and information about SMTP handy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM