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

时间:2014-05-23 01:11:06

标签: php html forms attachment phpmailer

我的网页上有联系表格,要求用户上传文件,一旦他们提交了表格,我希望它通过附件发送到我的电子邮箱。我已经想出如何将文件上传到uploads文件夹,我知道如何通过php发送附件,但我似乎无法将两者拼凑在一起。 (我正在使用PHP邮件程序发送电子邮件)。

我已经完成了一些教程和stackoverflow问题,但仍然陷入困境。

帮助?

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

联系人(HTML)

      <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

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

0 个答案:

没有答案