如何使用php电子邮件发送带有附件的电子邮件

时间:2020-01-20 21:15:21

标签: php email bootstrap-4 email-attachments

我有一个HTML引导表单,并且有一个php代码,用于发送表单信息的电子邮件。在发送电子邮件之前,需要先进行一次Google reCaptcha检查。

我从另一个有效的php形式复制了用于验证文本,发送电子邮件和重新验证的代码。

我如何使php代码以电子邮件附件的形式发送从表单引导表单接收到的2张图像。

这些是我的文件上传表单元素

<form role="form" class="form" action='https://opeturmo.com/parapente_ecuador/competencia_form.php' method='post'>
  <div class="form-group row">
    <div class="col-12"><label for="photo">Adjunte su foto - Add a photo of you</label></div>
    <div class="col-12"><p>IMPORTANTE: Adjunta una foto de tu cara. No de tu parapente o alguna otra cosa. Máx 10MB Tamaño: mayor de 400*400 px –IMPORTANT: A photo of your face. Not your glider or anything else. Max 10MB Size: not less than 400*400 px</p></div>
    <div class="col-12"><input type="file" class="form-control-file" id="photo" accept="image/*" name="my_file"></div>
  </div>
  <div class="form-group">
    <label for="pago">Adjunte su foto de pago - Add your payment photo</label>
    <p>IMPORTANTE: Adjunta una foto de tu cara. No de tu parapente o alguna otra cosa. Máx 10MB Tamaño: mayor de 400*400 px –IMPORTANT: A photo of your face. Not your glider or anything else. Max 10MB Size: not less than 400*400 px</p>
    <input type="file" class="form-control-file" id="pago" >
  </div>

这是我的PHP

<?php
//Checking For reCAPTCHA
$captcha;
if (isset($_POST['g-recaptcha-response'])) {
    $captcha = $_POST['g-recaptcha-response'];
}
// Checking For correct reCAPTCHA
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6LcPkZEUAAAAAL4DJYl-y3iOl1TdavmqhsghRXeU&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
if (!$captcha || $response.success == false) {
    header('location: ' . $_SERVER['HTTP_REFERER']);
} else {
/* Check all form inputs using check_input function */
function check_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

 if( isset($_POST) ){     
    //user sumbission data
    $ipaddress = $_SERVER['REMOTE_ADDR'];
    $date_sent = date('d/m/Y');
    $time = date('H:i:s');

    //form data
    $email = check_input($_POST['email']);
    //photo
    $file_name = $_FILES['my_file']['photo'];

    $participant_number = check_input($_POST['participant_number']);    
    $firstName = check_input($_POST['firstName']);
    $lastName = check_input($_POST['lastName']);
    $country = check_input($_POST['country']);
    $sexo = check_input($_POST['sexo']);
    $date = check_input($_POST['date']);
    $direccion = check_input($_POST['direccion']);
    $tel = check_input($_POST['tel']);
    $equipo = check_input($_POST['equipo']);
    $homologacion = check_input($_POST['homologacion']);
    $sponsor = check_input($_POST['sponsor']);
    $licenciaFAI = check_input($_POST['licenciaFAI']);
    $civilId = check_input($_POST['civilId']);
    $team = check_input($_POST['team']);
    $camisa = check_input($_POST['camisa']);
    $seguro = check_input($_POST['seguro']);
    $seguronumber = check_input($_POST['seguronumber']);
    $emergencyContact = check_input($_POST['emergencyContact']);
    $emergencyNumber = check_input($_POST['emergencyNumber']);
    $sangre = check_input($_POST['sangre']);

    //pago foto
    $file_pago = $_FILES['file_pago']['pago'];



    //send email if all is ok
    $headers = "From: opeturmo@opeturmo.com" . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $emailbody = "<h2>Booking - Reservas</h2>
                  <p><strong>Email: </strong> {$email} </p>
                  <p><strong>Nuero participante: </strong> {$participant_number} </p>
                  <p><strong>Nombre: </strong> {$firstName} </p>
                  <p><strong>Apellido: </strong> {$lastName} </p>
                  <p><strong>Pais: </strong> {$country} </p>
                  <p><strong>Sexo: </strong> {$sexo} </p>
                  <p><strong>Fecha: </strong> {$date} </p>
                  <p><strong>Direccion: </strong> {$direccion} </p>
                  <p><strong>Telefono: </strong> {$tel} </p>
                  <p><strong>Marca equipo parapente: </strong> {$equipo} </p>
                  <p><strong>Sponsor: </strong> {$sponsor} </p>
                  <p><strong>Licencia Fai: </strong> {$licenciaFAI} </p>
                  <p><strong>Civil Id: </strong> {$civilId} </p>
                  <p><strong>Equipo con quien compute: </strong> {$team} </p>
                  <p><strong>Talla camisa: </strong> {$camisa} </p>
                  <p><strong>Compania Seguros: </strong> {$seguro} </p>
                  <p><strong>Poliza numero : </strong> {$seguronumber} </p>
                  <p><strong>Contacto de Emergencia : </strong> {$emergencyContact} </p>
                  <p><strong>Numero de Contacto de Emergencia : </strong> {$emergencyNumber} </p>
                  <p><strong>Tipo de Sangre : </strong> {$sangre} </p>

                  <p>This message was sent from the IP Address: {$ipaddress} on {$date_sent} at {$time}</p>";
    $body = "--$boundary\r\n";
        $body .="Content-Type: $file_type; name=".$file_name."\r\n";
        $body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
        $body .="Content-Transfer-Encoding: base64\r\n";
        $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
        $body .= $encoded_content;

               mail ("opeturmo@gmail.com","Formulario Competencia",$emailbody,$body, $headers);
               //redirect back to form
    header('location: ' . $_SERVER['HTTP_REFERER']);

        }   







}
?>

我对php的处理还不多,所以我对此一无所知。

0 个答案:

没有答案