jquery .post使用swiftmailer发送电子邮件

时间:2012-12-17 22:09:27

标签: php jquery post email-attachments swiftmailer

我正在尝试使用swiftmailer发送电子邮件(用于附件),我过去一直在使用以下jquery邮政编码:

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

这个用法工作正常,但现在由于某种原因它在切换到swiftmailer时停止工作..如果我有一个带有action =“contact.php”的页面然后它工作但是使用jquery post函数似乎不是..

这是完整的jquery代码:

$(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;
});

});

和我的php文件“contact.php”有这个:

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

?>

以下是测试链接:

使用jquery帖子:here(点击“应用”,然后会出现一个弹出窗口)

只是php:here

1 个答案:

答案 0 :(得分:0)

停止使用MailTransport。 MailTransport只需调用mail()即可。

众所周知,

mail()不可靠,因为大多数托管服务提供商都不会理智地配置他们的服务器。

改为使用SendmailTransportSmtpTransport。您的主机肯定有SMTP服务器或Sendmail安装。检查他们的文档,他们应该sendmail路径和有关SMTP的方便的信息。