I'm trying to send some emails using PHPMailer and I'd like to attach a pdf file to them. The name of this file has to be selected from the user into a select tag in html. In particular this is the code receiving the input
<select name="Attachment">
<?php
$files=scandir("/pathtotherightfolder/");
for($i=0;$i<count($files);$i++)
{
if(substr($files[$i],-3,3)=="pdf")
echo "<option value='".$files[$i]."'>".$files[$i]."</option>";
}
?>
<option value="Nessuno">Nessuno</option>
</select>
This input is received from another php file which will send the emails and it's working correctly except for the attachment part, I tried all the ways suggested by tutorials, documentation, forums like
1st:
$url="URL of the correct directory in my website".$_POST['Attachment'];
$messaggio->AddStringAttachment(file_get_content($url), $_POST['Attachment'], $encoding='base64' , $type='application/pdf');//this one returns FALSE and sends the email with a corrupted attachment, and sends a warning of bad request...adding $_POST['Attachment']=str_replace(" ","%20",$_POST['Attachment']); it still returns false but it doesn't send the email
Also adding
chunk_split(base64_encode(file_get_contents($url)));
2nd:
$url="Samepathofthehtml".$_POST['Attachment'];//this path is correct
$messaggio->AddAttachment(realpath($url), $_POST['Attachment'], $encoding='base64' , $type='application/pdf');//this one returns bool(false) and sends the email with no attachments
3rd:
require("fpdf.php");
require("fpdi.php");
$pdf = new FPDI();
$pageCount = $pdf->setSourceFile("Samepathasbefore".$_POST['Attachment']);
$tplIdx = $pdf->importPage(1);//just a try with one page
$pdf->AddPage();
$pdf->useTemplate($tplIdx, 10, 10, 90);
$doc = $pdf->Output('', 'S');//if I try $pdf->Output(); i see the pdf correctly on my browser (so i deduce all the previous instructions are correct)
//And in the sending code
$messaggio->AddStringAttachment($doc, $_POST['Attachment'], $encoding='base64' , $type='application/pdf');//I obtain the same return value of the 2nd
In the 1st and 3rd case I receive the Attachment but the content is corrupted, in the 2nd I receive the mail without the attachment. Does someone has already tried to do something similar? Any advice?