How to send an email with php with several attachments?

0

Hello, I have a system that sends mails with attachments but only works with a file, that is, it just sends a file.

this is my html code

<form id="" class="form" name="sentMessage" action='php/enviar_cv.php' method='post' target='_self' enctype="multipart/form-data" > <!-- form wrapper -->

                    <div class="row"> <!-- nested inner row -->

                        <!-- Input your name -->
                        <div class="col-md-4">
                            <div class="form-group"> <!-- Your name input -->
                                <input type="text" autocomplete="off" class="form-control" name="nombre" placeholder="Nombre *" id="name" required data-validation-required-message="Please enter your name.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>

                        <!-- Input your email -->
                        <div class="col-md-4">
                            <div class="form-group"> <!-- Your email input -->
                                <input type="email" autocomplete="off" class="form-control" name="email" placeholder="Correo Electróncio *" id="email" required data-validation-required-message="Please enter your email address.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>

                        <!-- Input your Phone no. -->
                        <div class="col-md-4">
                            <div class="form-group"> <!-- Your email input -->
                                <input type="text" autocomplete="off" class="form-control" name="telefono" placeholder="Teléfono (s) *" id="phone" required data-validation-required-message="Please enter your phone no.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>

                        <div class="col-md-12">
                            <div class="form-group"> <!-- Your email input -->
                                <input type="text" autocomplete="off" name="asunto" class="form-control" placeholder="Asunto" id="asunto" >
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                    </div><!-- end nested inner row -->

                    <!-- Message Text area -->
                    <div class="form-group"> <!-- Your email input -->
                        <textarea class="form-control" rows="7" placeholder="Mensaje" name="comentarios" id="message" required data-validation-required-message="Please enter a message."></textarea>
                        <p class="help-block text-danger"></p>
                        <div id="success"></div>
                    </div>
                         <div class="col-md-12">
                        <p style="float:left"><b>Si deseas asesoria inmediata adjunta tus archivos:</b> 
                            <input type='file' name='archivo1' id='archivo1' multiple="multiple"/></p>
                    <button type="submit" class="btn btn-primary tf-btn color">Enviar</button> <!-- Send button -->
                    </div>
                </form>

and this is my php code:

function form_mail($sPara, $sAsunto, $html, $sDe){
$bHayFicheros = 0;
$sCabeceraTexto = "";
$sAdjuntos = "";
if ($sDe)
    $sCabeceras = "From:".$sDe."\n";
else
    $sCabeceras = "";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$sCabeceras .= "MIME-version: 1.0\n";
$sCabeceras .= "Content-type: multipart/mixed;";
$sCabeceras .= "boundary=\"{$mime_boundary}\"\n";
$sCabeceras .= "Content-transfer-encoding: 7BIT\n";

$body_top = "--{$mime_boundary}\n";
$body_top .= "Content-type: text/html\n";
$body_top .= "Content-transfer-encoding: 7BIT\n";
$body_top .= "Content-description: Mail message body\n\n";
$sTexto = $body_top.$html;

//if ($bHayFicheros)
foreach ($_FILES as $vAdjunto){
    if ($vAdjunto["size"] > 0){
        $sAdjuntos .= "\n\n--{$mime_boundary}\n";
        $sAdjuntos .= "Content-type: ".$vAdjunto["type"].";name=\"".$vAdjunto["name"]."\"\n";;
        $sAdjuntos .= "Content-Transfer-Encoding: BASE64\n";
        $sAdjuntos .= "Content-disposition: attachment;filename=\"".$vAdjunto["name"]."\"\n\n";
        $oFichero = fopen($vAdjunto["tmp_name"], 'r');
        $sContenido = fread($oFichero, filesize($vAdjunto["tmp_name"]));
        $sAdjuntos .= chunk_split(base64_encode($sContenido));
        fclose($oFichero);
    }
}

$sTexto .= $sAdjuntos."\n\n";

return mail($sPara,$sAsunto,$sTexto,$sCabeceras);
 }

if (form_mail("[email protected]", $asunto, $body, "[email protected]"));}
    
asked by Sistemas MKT 26.08.2016 в 18:19
source

1 answer

1

I use the PHP mailer library and use this function:

function enviar_correo($destinatarios, $mail_asunto, $mail_contendio, $from, $from_name, $archivos_adjuntos_ruta,$archivos_adjuntos_temp){
$mail= new PHPMailer(); // defaults to using php "mail()"
$mail->CharSet = 'UTF-8';
$body= $mail_contendio;
$mail->IsSMTP(); // telling the protocol to use SMTP
$mail->Host = "tu.host.com"; // SMTP server
$mail->From = $from;
$mail->FromName = $from_name;
$mail->Subject = $mail_asunto;
$mail->MsgHTML($body);
$destinatarios=explode(",", $destinatarios);
if(!empty($destinatarios)){
foreach($destinatarios as $un_destinatario){
$mail->AddAddress($un_destinatario); //destinatarios
}
}else{
return false;
}
if(!empty($archivos_adjuntos_ruta)){
foreach($archivos_adjuntos_ruta as $archivo){
$mail->AddAttachment($archivo); // attachment
}
}
if(!empty($archivos_adjuntos_temp)){
foreach($archivos_adjuntos_temp as $nombrearchivo=>$contenidoArchivo){
$mail->AddStringAttachment($contenidoArchivo,$nombrearch ivo,'base64');
}
}
$mail->Timeout = 20;
if($mail->Send()) {
return array(true);
}else {
return array(false,"Mailer Error: ".$mail->ErrorInfo);
}
}
$archivos_adjuntos_ruta=array($path1,path2);
$archivos_adjuntos_temp=array(utf8_decode($strfile PDF)=>$strContenidoPdf,utf8_decode($strNomArch)=>$ strContenidoXml);
enviar_correo(...,array(),archivos_adjuntos_temp);//los archivos estan en variables temporales
enviar_correo(...,$archivos_adjuntos_ruta,array()) ;//los archivos estan en rutas en disco
enviar_correo(...,$archivos_adjuntos_ruta,archivos _adjuntos_temp);//ambas opciones al mismo tiempo

Calling it with several files is at the end of the code.

    
answered by 26.08.2016 в 18:23