Paypal IPN帮助。通过电子邮件发送文件附件

时间:2017-10-24 13:33:13

标签: php email pdf paypal

我对PHP知之甚少。我聘请了一位自由职业者为我创建这个,然后在我注意到一个问题后,他的帐户在freelancer.com上关闭了。所以我再也无法与他沟通了。所以任何帮助都非常感激。他为我创建了一个系统,可以自动将数字文件通过电子邮件发送给eBay客户。它使用PayPal IPN的数量和价格信息来做到这一点。我的所有数字文件都是一次性使用。因此,一旦在eBay上购买文件,它就会通过电子邮件发送给客户,然后被丢弃。当客户订购数量为1时,系统运行良好。

问题是当客户订单超过1时.2个情况发生:例如,如果客户订购了2个数字文件,请说“XB1.pdf”和“XB2.pdf”。方案1他们将在电子邮件中获得“XB1.pdf”,有时在第二封电子邮件中获得“XB2.pdf”,但电子邮件中充满了一堆giberish(我将电子邮件内容发布到ipn.php的底部)内容如下)。场景2有时它们会收到2封电子邮件但两者都只包含“XB1.pdf”而不包含“XB2.pdf”。你能看一下代码,看看你是否能发现问题?这是ipn.php文件的内容,我相信这就是问题所在。

ipn.ph内容

<?php namespace Listener;

include('config.php');

// Set this to true to use the sandbox endpoint during testing:
$enable_sandbox = true;

// Set this to true to send a confirmation email:
$send_confirmation_email = true;




require('PaypalIPN.php');
use PaypalIPN;
$ipn = new PaypalIPN();
if ($enable_sandbox) {
    $ipn->useSandbox();
}
$verified = $ipn->verifyIPN();
$no_of_items=1;
$data_text = "";
foreach ($_POST as $key => $value) {
    $data_text .= $key . " = " . $value . "\r\n";
    if($key == "quantity"){
    $no_of_items=$value;
    }
    if($key == "payment_gross"){
    $total_payment=$value;  
    }
    if($key == "payer_email"){
    $payer_email=$value;    
    }
    if($key == "payment_status"){
    $payment_status=$value; 
    }

}
mail("", "sub", $data_text);


if ($send_confirmation_email) {
    // Send confirmation email
$total_payment=$total_payment/$no_of_items;

mail("", "total_payment", $total_payment);

$sql_s = "select * from settings";
$result_s = $conn->query($sql_s);
if ($result_s->num_rows > 0) {
while($query2_s = $result_s->fetch_assoc()) {

$email_from=$query2_s['email_from'];    
$email_subject=$query2_s['email_subject'];
$email_toa=$query2_s['email_to'];
$email_message=$query2_s['email_message'];
//$fileatt_name=$query2_s['fileame'];
$mail_me=$query2_s['mail_me'];  
}}

mail("", "no_of_items", $no_of_items);

$sql = "SELECT * FROM coupons where price='$total_payment' AND status=0 OR status=2 LIMIT ".$no_of_items."";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {

$status=$row['status'];     
if($status == 0 || $status == 2){
$file=$row['file']; 
$fileatt_name=$file;
$id=$row['id'];     
$fileatt = "files/".$file;
mail("", "file", $file);

            $fileatt_type = "application/pdf";
            $email_to =  $payer_email;
            $headers = "From: ".$email_from;
            $file = fopen($fileatt,'rb');
            $data = fread($file,filesize($fileatt));
            fclose($file);
            $semi_rand = md5(time());
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
            $headers .= "\nMIME-Version: 1.0\n" .
            "Content-Type: multipart/mixed;\n" .
            " boundary=\"{$mime_boundary}\"";
            $email_message .= "This is a multi-part message in MIME format.\n\n" .
            "--{$mime_boundary}\n" .
            "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\n\n" .
            $email_message .= "\n\n";
            $data = chunk_split(base64_encode($data));
            $email_message .= "--{$mime_boundary}\n" .
            "Content-Type: {$fileatt_type};\n" .
            " name=\"{$fileatt_name}\"\n" .
            "Content-Disposition: attachment;\n" .
            " filename=\"{$fileatt_name}\"\n" .
            //"Content-Transfer-Encoding: base64\n\n" .
            $data .= "\n\n" . "--{$mime_boundary}--\n";
            $ok = @mail($email_to, $email_subject, $email_message, $headers);
            if($email_toa != ""){
            $oka = @mail($email_toa, $email_subject, $email_message, $headers);
            }

if($status == 0){
$sql = "UPDATE coupons SET status='1',sent_to='$payer_email' WHERE id='$id'";
if ($conn->query($sql) === TRUE) {}
}else if($status == 2){
if($row['sent_to'] != ""){  
$payer_email=$row['sent_to'].",".$payer_email;
}
$sql = "UPDATE coupons SET sent_to='$payer_email' WHERE id='$id'";
if ($conn->query($sql) === TRUE) {}

}


}}}else {   
$oka = @mail($mail_me, "No File Found", "HI,<br>".$payer_email." has made a payment of ".$total_payment." but coupon could not be found.");


    }


}

// Reply with an empty 200 response to indicate to paypal the IPN was received correctly
header("HTTP/1.1 200 OK");

垃圾邮件内容,这只是其中的一部分,因为它很长

  

在这里写下你想要的任何东西。这将是电子邮件的正文   已发送。这是MIME格式的多部分消息。    - == Multipart_Boundary_xad2df9824fad4231989b39a4240b752ex Content-Type:text / html;字符集= “ISO-8859-1”   Content-Transfer-Encoding:7bit在这里写任何你想要的东西。这个   将是发送的电子邮件的正文。    - == Multipart_Boundary_xad2df9824fad4231989b39a4240b752ex Content-Type:application / pdf; NAME = “566222_XL-48Hour_3.pdf”   内容 - 处理:附件;文件名= “566222_XL-48Hour_3.pdf”   JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFu   Zyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDEyIDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4 +   Pg0KZW5kb2JqDQoyIDAgb2JqDQo8PC9UeXBlL1BhZ2VzL0NvdW50IDEvS2lkc1sgMyAwIFJdID4 +   DQplbmRvYmoNCjMgMCBvYmoNCjw8L1R5cGUvUGFnZS9QYXJlbnQgMiAwIFIvUmVzb3VyY2VzPDwv   Rm9udDw8L0YxIDUgMCBSL0YyIDkgMCBSPj4vRXh0R1N0YXRlPDwvR1M3IDcgMCBSL0dTOCA4IDAg   Uj4 + L1Byb2NTZXRbL1BERi9UZXh0L0ltYWdlQi9JbWFnZUMvSW1hZ2VJXSA + Pi9NZWRpYUJveFsg   MCAwIDYxMiA3OTJdIC9Db250ZW50cyA0IDAgUi9Hcm91cDw8L1R5cGUvR3JvdXAvUy9UcmFuc3Bh   cmVuY3kvQ1MvRGV2aWNlUkdCPj4vVGFicy9TL1N0cnVjdFBhcmVudHMgMD4 + DQplbmRvYmoNCjQg   MCBvYmoNCjw8L0ZpbHRlci9GbGF0ZURlY29kZS9MZW5ndGggMTc1Nz4 + DQpzdHJlYW0NCniclVht   b5tIEP4eKf9h ++ UEp8uGXXYXqKpKbdKmOTXXKPFVkar7QGzsoNjGApzU // 5mBkjA9bprVW0NzM7r

1 个答案:

答案 0 :(得分:0)

问题是代码在每个循环上附加到$email_message。第一次通过这很好;第二次通过一组新的标题和数据附加到原始电子邮件消息,这会破坏事情。

要更详细地查看问题,请按照代码进行操作。该行设置了msg的初始内容:

$email_message = $query2_s['email_message'];

内容来自数据库,从您包含的输出中我们可以看到它:

  

在这里写下你想要的任何东西。这将是发送的电子邮件的正文。

接下来,代码选择要处理的记录,并开始迭代它们。它通过将一些数据附加到上面准备的现有内容来构造消息:

$email_message .= "This is a multi-part message in MIME format.\n\n"
// etc, there are several lines of headers added here

最后,使用在最后几个步骤中构建的内容发送邮件:

$ok = @mail($email_to, $email_subject, $email_message, $headers);

一切都很好,第一封邮件的内容很好。但是,如果有多个项目,代码现在循环并再次运行下一条记录。这就是问题所在 - $email_message不会重置为空字符串,或者最初&#34;写下你想要的任何东西......&#34;来自数据库的消息。而是将第二组标题添加到现有消息内容中:

$email_message .= "This is a multi-part message in MIME format.\n\n"
// etc, appends new stuff to existing $email_message

$email_message现在包含邮件1的标题和内容,其中附有邮件2的标题和内容。

要避免这种情况,请使用不同的变量名称。例如,而不是这一行:

$email_message = $query2_s['email_message'];

使用:

$email_prefix = $query2_s['email_message'];

然后代替:

$email_message .= "This is a multi-part message in MIME format.\n\n" .

使用:

$email_message = $email_prefix . "This is a multi-part message in MIME format.\n\n" .

这种方式$email_message是在每次迭代时从头开始创建的,使用数据库中的$email_prefix并附加其余信息。