Use php mail () embedding a file?

1
   $to = $email;
    $subject = "Pedido #".$id_factura;

    //se puede hacer esto porque el mail se manda pero no sale este 
     //archivo

   $message = file_get_contents('../prueba.html',true);

  $headers = "Content-type: text/html\r\n";
  $headers .= "From: [email protected]";


   mail($to, $subject, $message,$headers);

or how can I send an email that goes with another file?

    
asked by Vinicio Moya Almeida 15.11.2017 в 18:28
source

2 answers

1

I recommend using the PHPMailer library, so you can send emails more easily, the official page is the following: link

Using this library, you can attach a file through a line:

$email->AddAttachment();

The complete example would be the following:

$email = new PHPMailer();
$email->From      = '[email protected]';
$email->FromName  = 'Mi Nombre';
$email->Subject   = 'Asunto';
$email->Body      = $textoDelMensaje;
$email->AddAddress( '[email protected]' );

$file_to_attach = 'RUTA_DEL_ARCHIVO_AQUI';

$email->AddAttachment( $file_to_attach , 'Archivo.pdf' );
    
answered by 15.11.2017 в 21:51
0
require("class.phpmailer.php");
$mail = new PHPMailer();

//Luego tenemos que iniciar la validación por SMTP:
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = ""; // A RELLENAR. Aquí pondremos el SMTP a utilizar. Por ej. mail.midominio.com
$mail->Username = ""; // A RELLENAR. Email de la cuenta de correo. [email protected] La cuenta de correo debe ser creada previamente. 
$mail->Password = ""; // A RELLENAR. Aqui pondremos la contraseña de la cuenta de correo
$mail->Port = 465; // Puerto de conexión al servidor de envio. 
$mail->From = ""; // A RELLENARDesde donde enviamos (Para mostrar). Puede ser el mismo que el email creado previamente.
$mail->FromName = ""; //A RELLENAR Nombre a mostrar del remitente. 
$mail->AddAddress("correo"); // Esta es la dirección a donde enviamos 
$mail->IsHTML(true); // El correo se envía como HTML 
$mail->Subject = “Titulo”; // Este es el titulo del email. 
$body = “Hola mundo. Esta es la primer línea ”; 
$body .= “Aquí continuamos el mensaje”; $mail->Body = $body; // Mensaje a enviar. $exito = $mail->Send(); // Envía el correo.
if($exito){ echo ‘El correo fue enviado correctamente.’; }else{ echo ‘Hubo un problema. Contacta a un administrador.’; } 

Use PHPMailer has functions such as:

  • Shipments to several senders, with CC, BCC, etc.
  • Supports 8bits, base64 and binaries
  • SMTP authentication on unencrypted ports 25, 587 TLS, 465 SSL
  • Submissions with HTML
  • Sending emails with attachments
  • Inclusion of images in the mail, etc.

There are more possibilities to add as attachments and hidden copies. Check the official manual here in your library git

    
answered by 15.11.2017 в 22:40