使用PhpMailer发送附件,其中包含用户选择的路径

时间:2015-10-21 06:49:12

标签: php email pdf phpmailer fpdf

我正在尝试使用PHPMailer发送一些电子邮件,我想将pdf文件附加到他们身上。必须从用户中选择此文件的名称为html中的选择标记。特别是这是接收输入的代码

<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>

此输入是从另一个php文件收到的,该文件将发送电子邮件并且除了附件部分之外它正常工作,我尝试了教程,文档,论坛等建议的所有方式

第一

      $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

同时添加

chunk_split(base64_encode(file_get_contents($url)));

第二

$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

第三

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

在第一和第三种情况下,我收到附件但内容已损坏,在第二种情况下,我收到没有附件的邮件。 有人已经尝试过做类似的事吗?有什么建议吗?

0 个答案:

没有答案