PHP附加PDF邮件功能 [英] PHP Attach PDF mail function

查看:51
本文介绍了PHP附加PDF邮件功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试让php发送带有PDF附件的电子邮件,我一直到处寻找并遇到了这篇文章

I have been trying to get php to send a email with a attached PDF, I have been looking everywhere and came across this post Email PDF Attachment with PHP Using FPDF.

我尝试了解决方案,但还是没有运气,有人知道我要去哪里了吗?我没有收到任何错误或电子邮件?

I tried the solution but still no luck, does anyone know where I am going wrong? I get no errors or email?

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

Basically what this is doing is grabbing values from a form, putting them into a database then sending a email to them.

任何帮助将不胜感激.

Any help would be greatly appreciated.

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.";
 }

}

推荐答案

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

I would move your antispam check up to the top, but I got the code below to work. I changed your EOL tag to "\r\n" and also added another $eol at the end of the header. Also I changed $message to $body in the mail() call, as all the prep work was done for $body but it wasn't used.

// 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";

这篇关于PHP附加PDF邮件功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆