2014-10-28 33 views
-2

单击按钮时,会生成PDF并在Web浏览器中打开PDF。我希望将pdf临时存储在变量中,并在点击按钮时将其作为附件包含在邮件中。在php中创建并发送临时pdf文件

我正在努力如何保存和附加文件。

<a class="button" href="http://mydomian.com/pdf001.php">Send PDF</a> 
<?php 
    $to  = '[email protected]'; 
    $subject = 'the subject'; 
    $message = 'hello'; 
    $headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 
    mail($to, $subject, $message, $headers); 
?> 
+0

使用PHPMailer和它的AttachFromString方法(无论它被称为 - 它没有在网站上明确记录,但确实存在) – 2014-10-28 18:47:15

+0

或者上级[SwiftMailer](http://swiftmailer.org/)库 – sjagr 2014-10-28 18:48:15

回答

1

就像在你的原帖评论说

使用PHPMailer的

<?php 
require_once('../class.phpmailer.php'); 

$mail    = new PHPMailer(); // defaults to using php "mail()" 

$mail->AddReplyTo("[email protected]","First Last"); 

$mail->SetFrom('[email protected]', 'First Last'); 

$mail->AddReplyTo("[email protected]","First Last"); 

$address = "[email protected]"; 
$mail->AddAddress($address, "John Doe"); 

$mail->Subject = "Php send pdf test"; 

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 

$mail->MsgHTML($body); 

$mail->AttachFromString (base64_encode(file_get_contents($urlToPdf)));  // attachment 
// in your case it would be http://mydomian.com/pdf001.php 


if(!$mail->Send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 
?> 

我编辑从网上房地产的快速这段代码,认为可以工作。与您调整每个变量。