duanpi7578 2015-10-21 06:49
浏览 55

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

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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)