【问题标题】:jquery .post send email using swiftmailerjquery .post 使用 swiftmailer 发送电子邮件
【发布时间】:2012-12-17 22:09:27
【问题描述】:

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

【问题讨论】:

  • 当你说它不起作用时,你是什么意思?服务器不响应 AJAX 调用? AJAX 调用响应成功代码但未发送电子邮件?您是否进行了任何调试以试图缩小问题范围?
  • 我没有收到错误代码,它的行为就像它已发送一样,但我没有收到电子邮件。我已将我的问题更新为我正在测试的链接

标签: php jquery post email-attachments swiftmailer


【解决方案1】:

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

mail() 是出了名的不可靠,因为大多数托管服务提供商都懒得合理配置他们的服务器。

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

【讨论】:

  • 我将其更改为 smtptransport 但它仍然执行相同的操作,就像它发送但我没有收到电子邮件一样。但如果我尝试不使用 jquery post 功能,它会完美发送。
  • 这没有任何意义。您确定没有发生您未检查的错误吗?也许是未捕获的异常?
猜你喜欢
  • 1970-01-01
  • 2015-04-16
  • 1970-01-01
  • 2016-04-04
  • 2010-12-11
  • 2012-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多