当前位置: 首页 > 知识库问答 >
问题:

如何下载文件并立即导出为电子邮件附件

司马建柏
2023-03-14

我有一个网络应用程序,用户可以点击按钮下载PDF报告。

我的用户要求在下载PDF时立即将其作为电子邮件附件打开(有点像单击mailto锚时)。

这可能吗?我在想也许可以在幕后使用js生成锚定标记,但我读到mailto并不真正支持附件。

如果这很重要,PDF将使用PHP mPDF设置为下载输出模式在服务器端生成。

共有2个答案

帅煌
2023-03-14

为此,您必须将过程分为两步。

  1. 使用http://wkhtmltopdf.org/在某个目录的服务器上生成PDF
  2. 使用https://github.com/PHPMailer/PHPMailer发送生成的pdf作为电子邮件附件的一部分

配置这两项服务将帮助您完成任务。

下面是将帮助您的示例代码

//generate PDF file for bill and send it in email
public function sendInvoiceInEmail($register_no, $html_string, $guest_email_id)
{
    //create mailer class
    $mail = new PHPMailer;
    //remove old invoice file if exist at public/doc
    // here i have used my path you have to use your path 
    if(file_exists(__DIR__.'/../../public/docs/invoice.pdf')){
        unlink(__DIR__.'/../../public/docs/invoice.pdf'); 
    }

    // You can pass a filename, a HTML string, an URL or an options array to the constructor
    $pdf = new Pdf([
        'commandOptions' => [
            'useExec' => false,
            'escapeArgs' => false,
            'procOptions' => array(
            // This will bypass the cmd.exe which seems to be recommended on Windows
            'bypass_shell' => true,
            // Also worth a try if you get unexplainable errors
            'suppress_errors' => true,
            ),
        ],
    ]);
    $globalOptions = array(
        'no-outline'
    );

    $pdf->setOptions($globalOptions);

    $pdf->addPage($html_string);
    //for Windows in my system i have setup wkhtmltopdf here
    $pdf->binary = '"C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe"';
    // for linux when you will host you have to setp on linux abd comment windows path and uncomment below line
    //$pdf->binary = '/home/username/wkhtmltox/bin/wkhtmltopdf';
    if (!$pdf->saveAs(__DIR__.'/../../public/docs/invoice.pdf')) {
        throw new Exception('Could not create PDF: '.$pdf->getError());
    }
    //now send email and attach invoice.pdf file from public/doc/invoice.pdf



    //$mail->SMTPDebug = 3;                               // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'hostname';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'username';                 // SMTP username
    $mail->Password = 'password';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to
    $mail->setFrom('from_email_address', 'From Name',FALSE);
    $mail->addAddress('to_email_address');     // Add a recipient

    $mail->addAttachment(__DIR__.'/../../public/docs/invoice.pdf');         // Add attachments

    $mail->isHTML(true);                                  // Set email format to HTML

    $mail->Subject = 'Subject';
    $mail->Body    = 'email message body';

    $is_email_sent = false;
    if(!$mail->send()) {
        // echo 'Message could not be sent.';
        // echo 'Mailer Error: ' . $mail->ErrorInfo;
        $is_email_sent = false;
    } else {
        // echo 'Message has been sent';
        $is_email_sent = true;
    }

    return true;
}
岳劲
2023-03-14

mailto如果文件在本地可用,则支持附件。您可以使用attach参数来指定
例如mailto:lastname。firstname@xxx.com?主题=测试

 类似资料:
  • 问题内容: 我在理解如何使用Python通过电子邮件发送附件时遇到问题。我已成功通过电子邮件将简单消息通过电子邮件发送。有人可以在电子邮件中说明如何发送附件。我知道在线上还有其他帖子,但是作为Python初学者,我很难理解它们。 问题答案: 这是另一个:

  • 问题内容: 我正在使用sendgrid发送电子邮件,并且使用以下代码可以正常工作,但没有附件。 但是我需要发送附件,因此我搜索了github源和Web文档API,由于某种原因,没有javadocs,但是有一个示例GitHub sendgrid, 所以我一直在尝试直到它起作用为止,我缩小了一些异常和响应代码,起初我是被禁止的未经授权,最好是响应202,表示有效且已排队(在此处检查),这是我的代码发送

  • 我在网上看到了很多代码,但它们似乎都遇到了问题。 使用以下功能创建并保存文件: 然而,当运行下面的代码来发送文件时,我一直遇到问题,并建议使用此链接的所有答案https://stackoverflow.com/questions/48117511/exposed-beyond-app-through-clipdata-item-geturi,当打开Gmail时,它说无法附加文件 如果有任何方法可以

  • 我已经成功创建了PDF文件,它存储在内部storage/app_name/files/xyz.PDF中,现在我只想在Gmail的附件中分享那个PDF......更新:在调试器中获取错误“源代码与字节码不匹配” 正在获取异常文件:///storage/emulated/0/storage/emulated/0/android/data/com.example.app_name/files/s.pdf

  • 如何从内容类型为“多部分/备选”的邮件中下载附件;

  • 问题内容: 我想发送带有PDF附件的电子邮件。我创建了PDF文件,然后执行了以下操作,但我认为这是错误的: 在发送电子邮件之前,我可以看到附件,但是当我发送电子邮件时,它的发送没有附件,这是因为我没有正确附加文件。 问题答案: 您错了。使用代替。