Search code examples
phphtmlemailemail-attachments

how to send different email with the same form?


i've a form that generates a pdf with fpdf and i want that this form send two different email. one to the client that has compiled the form (like a thank you massage) and a second email to me in order to answer to my client. I've this code that generates the pdf and send the email to a no-reply mail:

<?php
require('fpdf181/fpdf.php');


if(isset($_POST['submit'])){
    $id_appartamenti = $_POST['id_appartamenti'];
    $taglio = $_POST['taglio'];
    $style = $_POST['style'];
    $piano = $_POST['piano'];
    $mq = $_POST['mq'];
    $mood = $_POST['mood'];
    $finiture = $_POST['finiture'];
    $prezzo = $_POST['prezzo'];
    $nome = $_POST['nome'];
    $email = $_POST['email'];
    
    //A4 width: 219 mm
    //default margin: 10mm each side
    //writable horizontal: 219 - (10*2)=189mm
    $pdf = new FPDF('p', 'mm', 'A4');
    
    $pdf->AddPage();

    //set font to arial, bold, 14pt
    $pdf->SetFont('Arial','B', 14);

    //cell(width, height, text, border, endline, align)
    
    //titolo
    $pdf->Image('../images/favicon-outline-grigia.png',95, 2, 20, 20,0,1);    
    $pdf->Cell(0,30,'Buongiorno '.$nome.', ecco la configurazione del tuo '.$taglio.' '.$style.':',0,1,'C');
    
    //img
    $pdf->Cell(0,120,'',0,1);
    $pdf->Image('../images/bilocale1.jpg',20, 40, 160, 120,0,1);

    //corpo   
    //set font to arial, reg, 12pt
    $pdf->SetFont('Arial','', 10); 

    
    //riga 1
    $pdf->SetFillColor(230,230,230);
    $pdf->Cell(130,8,'ID appartamento:',0,0,'L', true);
    $pdf->Cell(59,8,''.$id_appartamenti.'',0,1,'L', true);//end of line
    
    //riga 2
    $pdf->SetFillColor(255,255,255);
    $pdf->Cell(130,8,'Piano:',0,0,'L', true);
    $pdf->Cell(59,8,''.$piano.'',0,1,'L', true);//end of line
    
    //riga 3
    $pdf->SetFillColor(230,230,230);
    $pdf->Cell(130,8,'Mq:',0,0,'L', true);
    $pdf->Cell(59,8,''.$mq.'',0,1,'L', true);//end of line  
    
    //riga 4
    $pdf->SetFillColor(255,255,255);
    $pdf->Cell(130,8,'Mood:',0,0,'L', true);
    $pdf->Cell(59,8,''.$mood.'',0,1,'L', true);//end of line  
    
    //riga 5
    $pdf->SetFillColor(230,230,230);
    $pdf->Cell(130,8,'Finiture:',0,0,'L', true);
    $pdf->Cell(59,8,''.$finiture.'',0,1,'L', true);//end of line
    
    //riga 5
    //set font to arial, bold, 14pt
    $pdf->SetFont('Arial','B', 20);    
    $pdf->SetTextColor(234, 103 , 12);
    $pdf->SetFillColor(255,255,255);
    $pdf->Cell(130,50,'',0,0,'R', true);
    $pdf->Cell(59,50,'Totale: '.$prezzo.' Euro',0,1,'R', true);//end of line  
    

    
////////////////////////////////////////////////////////////prima mail al cliente
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
    
    
      // email stuff (change data below)
    $to = "$email"; 
    $from = "noreply@tecmasolutions.com"; 
    $subject = "Tecma - la tua configurazione"; 
    $message = "<p>Buongiorno $nome, in allegato potrai trovare le tua configurazione</p>";

    // a random hash will be necessary to send mixed content
    $separator = md5(time());

    // carriage return type (we use a PHP end of line constant)
    $eol = PHP_EOL;

    // attachment name
    $filename = "$id_appartamenti.pdf";

    // encode data (puts attachment in proper format)
    $pdfdoc = $pdf->Output("", "S");
    $attachment = chunk_split(base64_encode($pdfdoc));

    // main header (multipart mandatory)
    $headers = "From: ".$from.$eol;
    $headers .= "MIME-Version: 1.0".$eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;

    //NOTICE I changed $headers to $body!!

    $body .= "Content-Transfer-Encoding: 7bit".$eol;
    $body .= "This is a MIME encoded message.".$eol; //had one more .$eol

    // message
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $body .= $message.$eol; //had one more .$eol

    // attachment
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
    $body .= "Content-Transfer-Encoding: base64".$eol;
    $body .= "Content-Disposition: attachment".$eol.$eol;
    $body .= $attachment.$eol;
    $body .= "--".$separator."--";
  

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

////////////////////////////////////////////////////////////seconda mail al cliente
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////    
    
    
       // email stuff (change data below)
    $to2 = "s.zanetti@tecmasolutions.com"; 
    $from2 = "$email"; 
    $subject2 = "Tecma - richiesta info per $id_appartamenti "; 
    $message2 = "<p>Una nuova richiesta da parte di $nome, per l'appartamento $id_appartamenti.</p>";

    // a random hash will be necessary to send mixed content
    $separator = md5(time());

    // carriage return type (we use a PHP end of line constant)
    $eol = PHP_EOL;

    // attachment name
    $filename = "$id_appartamenti.pdf";

    // encode data (puts attachment in proper format)
    $pdfdoc = $pdf->Output("", "S");
    $attachment = chunk_split(base64_encode($pdfdoc));

    // main header (multipart mandatory)
    $headers2 = "From: ".$from.$eol;
    $headers2 .= "MIME-Version: 1.0".$eol;
    $headers2 .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;

    //NOTICE I changed $headers to $body!!

    $body2 .= "Content-Transfer-Encoding: 7bit".$eol;
    $body2 .= "This is a MIME encoded message.".$eol; //had one more .$eol

    // message
    $body2 .= "--".$separator.$eol;
    $body2 .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $body2 .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $body2 .= $message.$eol; //had one more .$eol

    // attachment
    $body2 .= "--".$separator.$eol;
    $body2 .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
    $body2 .= "Content-Transfer-Encoding: base64".$eol;
    $body2 .= "Content-Disposition: attachment".$eol.$eol;
    $body2 .= $attachment.$eol;
    $body2 .= "--".$separator."--";   
    
    
 
    
/////////////////////////////////send message
/////////////////////////////////    
/////////////////////////////////    
/////////////////////////////////


    mail($to2, $subject2, $body2, $headers2);
    

}
header('Location: ../thank-you.html');
?>

How can i do to send another mail with another body but with the same attachement to another email (like myemail@mydomani.com)?


Solution

  • You could call the mail function again with a different $to parameter:

    $to  = "$email"; 
    $to2 = "secondemailaddress@email.com";
    // here a lot of your code
    
    mail($to, $subject, $body, $headers);
    
    mail($to2, $subject, $body, $headers);