如何使用php mailer将上传的文件从html表单发送到电子邮件?

如何使用php mailer将上传的文件从html表单发送到电子邮件?,php,html,forms,attachment,phpmailer,Php,Html,Forms,Attachment,Phpmailer,我在我的网页上有一个联系表格,要求用户上传一个文件,一旦他们提交了表格,我希望它与附件一起发送到我的电子邮件。我已经知道如何将文件上传到uploads文件夹,我知道如何通过php发送附件,但我似乎无法将两者结合起来。我使用PHP邮件发送电子邮件 我已经通过了一些教程和stackoverflow问题,但仍然卡住了 帮忙 *我是php新手,所以我可能遗漏了一些明显的东西 来自HTML的联系人 PHP 除非您想保留副本,否则不要移动它,只需从temp发送,当前您尝试附加的temp版本将不存在,而不是移

我在我的网页上有一个联系表格,要求用户上传一个文件,一旦他们提交了表格,我希望它与附件一起发送到我的电子邮件。我已经知道如何将文件上传到uploads文件夹,我知道如何通过php发送附件,但我似乎无法将两者结合起来。我使用PHP邮件发送电子邮件

我已经通过了一些教程和stackoverflow问题,但仍然卡住了

帮忙

*我是php新手,所以我可能遗漏了一些明显的东西

来自HTML的联系人

PHP


除非您想保留副本,否则不要移动它,只需从temp发送,当前您尝试附加的temp版本将不存在,而不是移动的副本。这是有意义的。如何发送临时版本?我知道使用php mailer时,您会添加附件$path,但我不确定路径应该是什么,因为它总是有不同的名称?
      <div class="citaform">
        <div class="col-md-6 centered">
          <form action="reclutamiento.php" method="POST" enctype="multipart/form-data">
            <input placeholder="Nombre * " type="text" name="Nombre" maxlength="40"/>
            <input placeholder="Apellido *" type="text" name="Apellido" maxlength="40"/>
            <input placeholder="Email *" type="text" name="Email" maxlength="100"/>
            <input placeholder="Teléfono 1 *" type="text" name="Telefono1" maxlength="9" pattern=".{8,}" required title="8 numeros mínimo"/>
            <input placeholder="Teléfono 2 *" type="text" name="Telefono2" maxlength="9" pattern=".{8,}" required title="8 numeros mínimo"/>
            <input placeholder="Departamento/Municipio *" type="text" name="Departamento" maxlength="100"/>
            <input placeholder="Zona *" type="text" name="Zona" maxlength="100"/>
            <input placeholder="Fecha de Nacimiento *" type="text" name="Cumpleanos" maxlength="100"/>
            <input placeholder="DPI *" type="text" name="DocumentoPersonal" maxlength="20"/>
            <input placeholder="Plaza *" type="text" name="Plaza" maxlength="100"/>
               <h5>Ingrese su CV (opcional)</h5>
            <input type="file" name="file" id="file" />
            <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
              <h5>¿Cual es su pretensión salarial?*</h5>
            <input placeholder=" " type="text" name="Salario" maxlength="50"/>
            <button class="btn-cita" name="aplicacion">Mandar Aplicación</button>
          </form>
        </div>
        </div><!-- /col-md-4 -->
      </div>
<?php 

 if(isset($_POST['aplicacion'])) {
 $Nombre_field = trim($_POST["Nombre"]);
 $Apellido_field = trim($_POST["Apellido"]);
 $Email_field = trim($_POST["Email"]);
 $Telefono1_field  = trim($_POST["Telefono1"]);
 $Telefono2_field  = trim($_POST["Telefono2"]);
 $Departamento_field  = trim($_POST["Departamento"]);
 $Zona_field  = trim($_POST["Zona"]);
 $Cumpleanos_field  = trim($_POST["Cumpleanos"]);
 $Documento_field  = trim($_POST["DocumentoPersonal"]);
 $Plaza_field  = trim($_POST["Plaza"]);
 $Salario_field  = trim($_POST["Salario"]);
 $Curriculum_field = $_FILES('file');


require_once("inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();

if (!$mail->ValidateAddress($Email_field)){
    echo "You must specify a valid email address.";
    exit;
}

$email_body = "";
$email_body = $email_body . "Nombre: " . $Nombre_field . "<br>";
$email_body = $email_body . "Apellido: " . $Apellido_field . "<br>";
$email_body = $email_body . "Email: " . $Email_field . "<br>";
$email_body = $email_body . "Telefono1: " . $Telefono1_field . "<br>";
$email_body = $email_body . "Telefono2: " . $Telefono2_field . "<br>";
$email_body = $email_body . "Departamento: " . $Departamento_field . "<br>";
$email_body = $email_body . "Zona: " . $Zona_field . "<br>";
$email_body = $email_body . "Cumpleanos: " . $Cumpleanos_field . "<br>";
$email_body = $email_body . "DPI: " . $Documento_field . "<br>";
$email_body = $email_body . "Plaza: " . $Plaza_field . "<br>";
$email_body = $email_body . "Salario: " . $Salario_field . "<br>"; 

 $allowedExts = array("doc", "docx", "pdf");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);
    if ((($_FILES["file"]["type"] == "application/pdf")
    || ($_FILES["file"]["type"] == "application/msword")

    && in_array($extension, $allowedExts))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "<script>alert('Error: " . $_FILES["file"]["error"] ."')</script>";
        }
      else
        {
            $d='uploads/';
            $de=$d . basename($_FILES['file']['name']);
                move_uploaded_file($_FILES["file"]["tmp_name"], $de);
                    $fileName = $_FILES['file']['name'];
                $filePath = $_FILES['file']['tmp_name'];
         //add only if the file is an upload
         }
      }
    else
      {
      echo "<script>alert('Invalid file')</script>";
      }


$mail->SetFrom($Email_field, $Nombre_field);
$address = "isabel@juntostudio.com";
$mail->AddAddress($address, "Reclutamiento");
$mail->Subject    = "Nuevo Candidato | " . $Nombre_field ;
$mail->MsgHTML($email_body);
$mail->addAttachment($Curriculum_field);         // Add attachments

    if(!$mail->Send()) {
  echo "Hubo un problema enviando el correo: " . $mail->ErrorInfo;
  exit;
}

header("Location: success3.html");
exit;

}
?> 

<?php