【问题标题】:Sending a file from form to email将文件从表单发送到电子邮件
【发布时间】:2016-04-09 02:48:52
【问题描述】:

我试图弄清楚如何将包含图像等内容的文件发送到文本文件,etx 从通过 ajax 的表单提交到我的 php 文件以发送到电子邮件中。我正在尝试按照我在 ajax 和 php 电子邮件中的其他数据的方式对其进行格式化,但我可以告诉表单在我的 ajax 中立即停止。它甚至没有发送到我的 php,但我不确定我的电子邮件部分是否正确。

这是我到目前为止所尝试的。我尝试删除尽可能多的过时代码,但仍然包含足以让我对我正在尝试做的事情产生良好感觉的代码。

如何使这个文件通过 AJAX 附加/发送到电子邮件到我的 php 电子邮件

<form action="" autocomplete="on" method="POST" id="project-information-form" enctype="multipart/form-data">
  <input type="text" class="input-borderless" id="project-name" name="name" placeholder="Your Name">
  <input type="email" class="input-borderless" id="project-email" name="email" placeholder="Email Address">
  <input type="number" class="input-borderless" id="project-number" name="phone" placeholder="Phone Number">
  <input type="file" name="file" id="file" class="inputfile" data-multiple-caption="{count} files selected" multiple>
  <label for="file"><span id="file-upload-image"><img src="/icons/white-upload.png" height="25px" width="25px"></span>File Upload</label>
  <input type="submit" id="submit-project" class="submit-project-button" value="Send Project Inquiry">
</form>

AJAX

   $("#submit-project").on("click", function(event) {
     // event.preventDefault();

      var project_name = $("#project-name").val();
      var project_email = $("#project-email").val();
      var project_number = $("#project-number").val();
      var uploaded_file = $("#file").val();
        submitHandler: function(form) {
console.log(form);
          $.ajax({
            url: "email-project.php",
            type: "POST",
            data: {
              "project_name": project_name,
              "project_email": project_email,
              "project_number": project_number,
              "file": project_file
            },
            success: function(data) {
              //console.log(data); // data object will return the response when status code is 200
              if (data == "Error!") {
                alert("Unable to send email!");
                alert(data);
              } else {
              }
            },

用于电子邮件的 PHP 页面

ini_set('display_errors', 1);
error_reporting(E_ALL);

$project_name           = $_POST['project_name'];
$project_email          = $_POST['project_email'];
$project_number         = $_POST['project_number'];
$project_file           = $_POST['file'];

$to = '';
$subject = '';
$message = '
    <html>
    <head>
        <title>Project Inquiry Form Sent</title>
    </head>
    <body>
        <p>There has been a Project submitted. Here are the details:</p><br>
        <p>Name: '. $project_name .'</p>
        <p>Email: '. $project_email .'</p>
        <p>Phone Number: '. $project_number .'</p>
        <p>The client uploaded a file ' .$project_file.'.</p>

    </body>
    </html>
';
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' .$project_email . "\r\n";

if (!empty($project_email)) { 
    if (filter_var($project_email, FILTER_VALIDATE_EMAIL)) { 

        //Should also do a check on the mail function
        if (mail($to, $subject, $message, $headers)) {
            echo "Your email was sent!"; // success message
        } else {
            echo "Mail could not be sent!"; // failed message
        }

    } else { 
        //Invalid email
        echo "Invalid Email, please provide a valid email address.";
    }

} else {
    echo "Email Address was not filled out.";
}

【问题讨论】:

  • 你知道 JSON 不能有一个保存文件值的属性吗?
  • 不,我没有。那我该怎么办呢?
  • 看看我在下面发布的示例。希望对你有帮助:)!

标签: javascript php jquery ajax email


【解决方案1】:

您需要在服务器上运行的脚本将文件移动到上传目录。 jQuery ajax 方法将表单数据发送到服务器,然后服务器上的脚本处理上传。

这是一个使用 PHP 的示例。看看这个例子。

信用在这里 -> jQuery AJAX file upload PHP

$('#submit-project').on('click', function() {
var file_data = $('#file').prop('files')[0];   
var form_data = new FormData();                  
form_data.append('file', file_data);
alert(form_data);                             
$.ajax({
            url: 'email-project.php', // point to server-side PHP script 
            dataType: 'text',  // what to expect back from the PHP script, if anything
            cache: false,
            contentType: false,
            processData: false,
            data: form_data,                         
            type: 'post',
            success: function(php_script_response){
                alert(php_script_response); // display response from the PHP script, if any
            }
 });
});

【讨论】:

  • 成功添加的项目会不会和我现在正在做的事情混为一谈?
猜你喜欢
  • 2012-07-02
  • 1970-01-01
  • 2012-03-15
  • 1970-01-01
  • 2019-03-07
  • 2016-07-02
  • 2014-02-22
  • 1970-01-01
相关资源
最近更新 更多