gpt4 book ai didi

fpdf - PHP附加PDF邮件功能

转载 作者:行者123 更新时间:2023-12-02 07:37:58 25 4
gpt4 key购买 nike

我一直在尝试让 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.";
}

最佳答案

我会把你的反垃圾邮件检查移到顶部,但我得到了下面的代码。我将您的 EOL 标记更改为“\r\n”,并在 header 末尾添加了另一个 $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";

关于fpdf - PHP附加PDF邮件功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14275172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com