2012-12-17 81 views
0

我試圖發送使用swiftmailer電子郵件(附着物)和我一直在使用,在過去下面的jQuery代碼後發送電子郵件:的jQuery。員額使用swiftmailer

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

這就要用到做工精細,但現在由於某種原因,它停止工作時,切換到swiftmailer ..如果我有一個頁面的動作=「contact.php」,那麼它的作品,但使用jquery後功能似乎不..

這裏是完整的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; 
}); 

}); 

和我的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('[email protected]' => 'no-reply')) 

// Set the To addresses with an associative array 
->setTo('[email protected]') 

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

+0

當你說它不起作用時,你是什麼意思?服務器不響應AJAX調用? AJAX調用迴應成功代碼,但沒有發送電子郵件?你有沒有做過任何調試來試圖縮小問題的範圍? –

+0

我沒有收到任何錯誤代碼,它的行爲就好像它已發送,但我沒有收到任何電子郵件。我已將我的問題更新到我正在測試的鏈接 – Andres

回答

0

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

mail()是臭名昭着的不可靠的,因爲大多數託管服務提供商不會打擾配置他們的服務器的健全。代替使用SendmailTransportSmtpTransport。您的主機肯定有SMTP服務器或Sendmail安裝。檢查他們的文檔,他們應該sendmail路徑和有關SMTP的信息方便。

+0

我將其更改爲smtptransport,但它仍然執行相同操作,就好像它發送了一樣,但我沒有收到電子郵件。但如果我嘗試沒有jquery post功能,它發送完美。 – Andres

+0

這沒有任何意義。你確定沒有發生你沒有檢查的錯誤嗎?也許一個未捕獲的異常? – Charles