【问题标题】:PHP Attach PDF mail functionPHP 附加 PDF 邮件功能
【发布时间】:2012-12-25 20:36:25
【问题描述】:

我一直在尝试让 php 发送带有附件 PDF 的电子邮件,我一直在到处寻找并发现了这个帖子 Email PDF Attachment with PHP Using FPDF

我尝试了解决方案,但仍然没有运气,有人知道我哪里出错了吗?我没有收到错误或电子邮件?

基本上,这是从表单中获取值,将它们放入数据库,然后向它们发送电子邮件。

任何帮助将不胜感激。

require('fpdf.php');
include("database.php");
require_once('recaptchalib.php');
$publickey = "6LcBYNsSAAAAAKkf5LwKkrhTVTVlxmOblcOn70-r ";
$privatekey = " 6LcBYNsSAAAAAEAa9wFZfyjInRmsWCxpj79Nvqm7";


// email stuff (change data below)
$to = "brentfrench@bigwavemedia.co.uk";
$from = "websupport@bigwavemedia.co.uk"; 
$subject = "send email with pdf attachment"; 
$message = "<p>Please see the attachment.</p>";


// PDF Create 
$pdf = new FPDF('P', 'pt', array(500,233));
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');


// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "test.pdf";

// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

// form values
$firstname = $_POST['first'];
$lastname = $_POST['last'];
$postcode = md5($_POST['post']);
$email = $_POST['email'];


if ($_POST["recaptcha_response_field"]) {

    $resp = recaptcha_check_answer ($privatekey,
                                    $_SERVER["REMOTE_ADDR"],
                                    $_POST["recaptcha_challenge_field"],
                                    $_POST["recaptcha_response_field"]);
    if ($resp->is_valid) {

                            $check = mysql_query("SELECT * FROM voucher WHERE first='$firstname' AND last='$lastname'");
                            if(mysql_num_rows($check) != 0)
                            {
                                echo "<p>Voucher already in use.</p>";
                            }
                            else
                            {   
                                $insert = 'INSERT into voucher(first, last, postcode, email) VALUES ("'.$firstname.'","'.$lastname.'","'.$postcode.'","'.$email.'")';
                                mysql_query($insert);   
                                // send message
                                mail($to, $subject, $message, $headers) or die("Mail Error");                   
                                echo "<p>Voucher has been emailed, we looked foward to seeing you soon</p>";
                            }
 }
 else
 {
    echo "The anti spam code you entered is not correct.";
 }

}

【问题讨论】:

  • 我会先执行你的反垃圾邮件检查 - 否则服务器正在为可能是垃圾邮件的东西做很多工作。
  • 为什么人们总是尝试使用 PHP 的 mail() 函数来创建复杂的电子邮件????第一百次,使用decent mailer class like phpMailer 来处理这种事情,并为自己节省数小时的挫败感

标签: fpdf php


【解决方案1】:

我会将您的反垃圾邮件检查移到顶部,但我得到了下面的代码。我将您的 EOL 标记更改为“\r\n”,并在标题末尾添加了另一个 $eol。此外,我在 mail() 调用中将 $message 更改为 $body,因为 $body 的所有准备工作都已完成,但并未使用。

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = "\r\n";

// attachment name
$filename = "test.txt";

// encode data (puts attachment in proper format)
$attachment = chunk_split(base64_encode("I am text file"));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;

// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

// form values
$firstname = $_POST['first'];
$lastname = $_POST['last'];
$postcode = md5($_POST['post']);
$email = $_POST['email'];


echo mail($to, $subject, $body, $headers) or die("Mail Error");                   
echo "<p>Voucher has been emailed, we looked foward to seeing you soon</p>\n";

【讨论】:

  • 替换了 $attachment = chunk_split(base64_encode("我是文本文件")); with chunk_split(base64_encode(file_get_contents($filename)));以获得更好的“'复制和粘贴'能力”。
【解决方案2】:

我写了一个邮件类。附件代码如下:

$body .= "--".$separator."\r\n";
$body .= "Content-Disposition: attachment; filename=\"".$filename."\";\r\n";
$body .= "Content-Length: ".$filesize.";\r\n";
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= $attachment."\r\n";
$body .= "--".$separator."--";

您会发现一些不同之处:我将 Content-Length、2x $filename\r\n 作为 EOL。

我希望这会有所帮助。

【讨论】:

  • 当我尝试这个时,我得到了这个错误(致命错误:不在对象上下文中使用 $this)
  • 好吧对不起..这是我班上的。将 $this-&gt;_body 替换为 $body 并将 $filesize 替换为 filesize()
  • 我已编辑答案并将$this-&gt;_body 替换为$body。面向对象编程时需要$thisphp oop
【解决方案3】:

这段代码中有很多可能的断点,无法准确判断出了什么问题。您需要自己进行一些调试,例如。使用测试输出,看看脚本能走多远。还要确保激活错误报告。

发送附件更简单的方法可能是使用SwiftMailer library.

【讨论】:

  • 我已尽我所知尝试过测试,一切看起来都还不错。我去看看谢谢
猜你喜欢
  • 2012-05-23
  • 1970-01-01
  • 2015-12-10
  • 1970-01-01
  • 1970-01-01
  • 2013-04-01
  • 2011-05-11
  • 1970-01-01
  • 2014-04-05
相关资源
最近更新 更多