0

我正在尝试使用 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:这里

4

1 回答 1

0

停止使用MailTransport。MailTransport 只是mail()在幕后调用。

mail()众所周知,它不可靠,因为大多数托管服务提供商都不会费心合理地配置他们的服务器。

请改用SendmailTransportSmtpTransport。您的主机肯定有 SMTP 服务器或 Sendmail 安装。查看他们的文档,他们应该将 sendmail 路径和有关 SMTP 的信息放在手边。

于 2012-12-17T22:59:13.010 回答