PayPal IPN Help (Not UPDATE Database) - paypal

Hi there all been trying to get this working for about 2 days now. The Payment is going through no problem but it not update the Database :(
Here is the Code :
<?php
require_once(WWW_DIR."/lib/users.php");
// Connect to database
$con = mysql_connect("localhost","root","******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value)
{
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); // Test
//$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); // Live
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$txn_id = $_POST['txn_id'];
$payer_email = $_POST['payer_email'];
$name = $_POST['first_name'] . " " . $_POST['last_name'];
$address_street = $_POST['address_street'];
$address_zip = $_POST['address_zip'];
$address_city = $_POST['address_city'];
$contact_phone = $_POST['contact_phone'];
$email = $_POST['payer_email'];
$uid = $users->currentUserId();
//timezone set
date_default_timezone_set('Europe/London');
if (!$fp)
{
// HTTP ERROR
} else
{
fputs($fp, $header . $req);
while (!feof($fp))
{
$res = fgets($fp, 1024);
if (strcmp($res, "VERIFIED") == 0) {
if($payment_status != "Completed"){
if($payment_amount == 2.00)
{
$role = 6;
}
else
if($payment_amount == 5.00)
{
$role = 8;
}
else
if($payment_amount == 8.00)
{
$role = 9;
}
//update user records
$sql = sprintf("UPDATE users SET role = %d WHERE ID = %d", $role, $uid);
mysql_query($sql);
//log payment
//mysql_query("INSERT INTO payments (email, item_name, payment_status, txn_id, payment_ammount) VALUES('" . mysql_escape_string($email) . "', '" . $item_name . "', '" . $payment_status . "', '" . $txn_id . "', '" . $payment_amount . "' ) ") or die(mysql_error());
}} else
if (strcmp($res, "INVALID") == 0)
{
// log for manual investigation
}
}
fclose($fp);
}
?>
If you have any Questions just ask I will try and Answer ASAP if you could get this working I would be very happy
Thanks

Change:
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
To:
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n";
$header .= "Connection: close\r\n\r\n";
Change:
if (strcmp($res, "VERIFIED") == 0) {
To:
if (strcmp(trim($res), "VERIFIED") == 0) {
And lastly, change:
if (strcmp($res, "INVALID") == 0)
To:
if (strcmp(trim($res), "INVALID") == 0)
Reason:
The PayPal Sandbox is configured to reject HTTP 1.0 requests without a Host header specified, so your current script will never properly validate.

Related

reply to sender mail php script

I have been looking for a mailform script and came up with his. Works fine, but just this matter: when I reply to the received email, I am replying to myself, whilst I would like to reply automatically to the mailadres of the sender.
<?php
$to = "MY#MAILADRES.COM";
$subject = "mail via website";
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$onderwerp = $_POST['onderwerp'];
$comments = $_POST['message'];
$message = "
name: $firstname $lastname
email: $email
subject: $onderwerp
message: $comments
";
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$headers = "From: MY#MAILADRES.COM\r\n".
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
foreach($_FILES as $userfile)
{
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
if (file_exists($tmp_name))
{
if(is_uploaded_file($tmp_name))
{
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
}
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
$message.="--{$mime_boundary}--\n";
if (mail($to, $subject, $message, $headers))
echo "Thanks for getting in touch.<br>Your message wil get my full attention.<br>I will get back to you soon.";
else
echo "Error in mail.<br>Please try again.";
?>
What should I change in this code, please?
I added these (seperately):
$headers .= "From: Contact Form (Domain.com) <no-reply#domain.com>\r\n";
$headers .= "Reply-To: ".$_REQUEST['email']."<". $_REQUEST['email'].">\r\n"
But none seem to work. I got this code to work with and all good, except for the reply-to.

php mail sending attachment the can't be opened

I'm trying to write a page where users can send an email along with an attachment. I think I'm almost there. Right now I get the email, but the attachment is either empty in filesize, or when I try to open it, I get a message that it cannot be opened. Any help is appreciated.
function valid_email($Email)
{
//new regex, didn't give me any errors...might be a bit more exact
if (ereg('^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$', $Email) != false)
return true;
else
return false;
}
function sendEmail($email){
return mail ($email['to'], $email['subject'], $email['message'], $email['headers']);
}
if ( strlen($_FILES['Resume_File']['name']) > 0 )
{ // If a file was uploaded, do some processing
$filename = preg_replace('/[^a-zA-Z0-9._-]/', '', $_FILES['Resume_File']['name']);
$filetype = $_FILES["Resume_File"]["type"];
$filesize = $_FILES["Resume_File"]["size"];
$filetemp = $_FILES["Resume_File"]["tmp_name"];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
if ( !preg_match('/\.(doc|docx|odt|pdf|rtf|txt)/i', $ext) )
{ $errors++;
$errorLog .= "Upload filetype not supported.";
}
else if ( $filesize > 2000000 )
{ $errors++;
$errorLog .= "File size too high, up to 2MB is allowed.";
}
else
{ // Looks like the file is good, send it with the email
//$fp = fopen($filetemp, "rb");
//$file = fread($fp, $filesize);
//$file = chunk_split(base64_encode($file));
//$email['headers'] .= "\n--{$num}\n";
//$email['headers'] .= "Content-Type:{$filetype}";
//$email['headers'] .= "name={$filename}r\n";
//$email['headers'] .= "Content-Disposition: attachment; ";
//$email['headers'] .= "filename={$filename}\n";
//$email['headers'] .= "{$file}";
}
}
// get posted data into local variables
$fname = trim(stripslashes($_POST['fname']));
$lname = trim(stripslashes($_POST['lname']));
$emailAddress = trim(stripslashes($_POST['email']));
$company = trim(stripslashes($_POST['company']));
$information = trim(stripslashes($_POST['information']));
$subject = trim(stripslashes($_POST['subject']));
$title = trim(stripslashes($_POST['title']));
//setup email
$to = 'me#me.com';
$subject = "Resume Submission";
$headers = "From: {$fname} {$lname} <{$emailAddress}>\r\n";
// prepare email body text
$message = "First Name: {$fname} <br>";
$message .= "Last Name: {$lname} <br>";
$message .= "Email: {$emailAddress} <br>";
$message .= "Title: {$title} <br><br>";
$message .= "Comments: {$information}";
if ( $errors == 0 ) {
// Attachment headers
//$to = "myemail#mydomain.com";
//$from = "Website <website#mydomain.com>";
//$subject = "Test Attachment Email";
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
//$filename = "document.pdf";
//$pdfdoc is PDF generated by FPDF
$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."--";
}
//sendEmail($email);
// validation
if ( $errors == 0 ) {
if (valid_email($emailAddress) && $fname != "" && $lname != "") { //if return is true...
mail($to, $subject, $body, $headers);
echo 0; //Success
}else { //otherwise
echo 1; //Error
}
} else {
echo 1; //Error
}
I am using portions of code which I found here
PHP mail() attachment problems
Thank you!
PHP's mail() function is really poor, particularly when trying to do advanced stuff like adding attachments. It's really only worth using for the most basic "send a notification email to the site admin" type emails, and even then it's not always the best solution.
I would suggest ditching any attempt to work with the mail() function itself, and instead switch to using a decent PHP mailer class such as the appropriately named phpMailer.
phpMailer makes creating emails from PHP dead easy. I has all the features you could want, including making it very easy to add attachments, write HTML emails, and plenty more.
But the best thing about phpMailer is that it removes all the need to waste dozens of lines of code formatting the email headers. All that stuff with the separators and mime types becomes reduced to a few simple lines of code. Easier to read, easier to maintain, and less likely to have bugs. You win all round.
//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));
$pdfdoc is not mentioned any where in the page. I presume that is the issue:|

Why did Implementing HTTP/1.1 break my IPN script?

Here is the original code I had in my PHP script:
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
I got an email from Paypal saying that I needed to upgrade my IPN script so that it uses HTTP/1.1. So here is what I changed my code to, based on their directions:
$header .="POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .="Content-Type: application/x-www-form-urlencoded\r\n";
$header .="Host: www.paypal.com\r\n";
$header .="Connection: close\r\n";
Payments have gone through today, but the IPN is no longer updating my database and this is the only change I made to it. Any ideas on what to do?
Thanks!
Yes. I have just done battle with this. What worked for me was to remove the connection close header, and add a trim to the response back from PP. Here are the headers:
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Host: www.paypal.com\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
Here is the fsockopen:
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
and here is the trim on the response back from PP:
if (!$fp) {
// HTTP ERROR
error_mail("Could not open socket");
//
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = trim(fgets ($fp, 1024));
}
//
// check the payment_status is Completed
// check that receiver_email is your Primary PayPal email
//
if ((strcmp ($res, "VERIFIED") == 0) && ($payment_status == "Completed") && ($receiver_email == $valid_receiver_email)) {
That worked for me.

PHP Sending Emails with File Attachments - Email Not Sending At All

After trying to read various articles on sending emails with attachments in PHP (I am use to ASP with VBScript), I wrote the code below. Unfortunately, it does not work at all. Not only does it not send the email with the attachment, the email doesn't seem to send at all, even though my script says that it did send. Where have I gone wrong? I'm not using a form to upload a file. This is a static script.
<?php
$EmailTo = "Me#here.com";
$EmailFrom = "You#There.com";
$EmailSubject = "The Email Subject";
$MailBoundary = md5(uniqid(time()));
$Headers = "To: ". $EmailTo . "\r\n";
$Headers .= "From: ". $EmailFrom . "\r\n";
$Headers = "MIME-Version: 1.0\r\n";
$Headers .= "Content-type: multipart/mixed;boundary=\"$MailBoundary \"";
$Headers .= "\r\n\r\n";
$Headers .= "This is a multi-part message in MIME format.";
$Headers .= "\r\n\r\n";
$FileAttachment = "AttachedFile.pdf";
$File = fopen($FileAttachment, "r");
$FileData = fread($File, filesize($FileAttachment));
$FileData = chunk_split(base64_encode($FileData));
$FileName = basename($FileAttachment);
$EmailBody = "--$MailBoundary\r\n";
$EmailBody .= "Content-type: text/html; charset=iso-8859-1\r\n";
$EmailBody .= "Content-transfer-encoding: 8bit\r\n\r\n";
$EmailBody .= "<html>" . chr(13) .
"<head>" . chr(13) .
"<style>" . chr(13) .
".breg {font-family:arial;font-size:10pt;color:#000000;padding:5px;}" . chr(13) .
"</style>" . chr(13) .
"</head>" . chr(13) .
"<body>" . chr(13) .
"<div class=" . chr(34) . "breg" . chr(34) . ">" . chr(13) .
"The message text body goes here" . chr(13) .
"</div>" . chr(13) .
"</body>" . chr(13) .
"</html>";
$EmailBody .= "--$MailBoundary\r\n";
$EmailBody .= "Content-type: " . mime_content_type($File) . "; name=$FileName\r\n";
$EmailBody .= "Content-transfer-encoding:base64\r\n\r\n";
$EmailBody .= $FileData. "\r\n\r\n";
$EmailBody .= " --$MailBoundary--";
if (mail($EmailTo, $EmailSubject, $EmailBody, $Headers))
{
echo "Email to " . $EmailTo . " has been sent" . chr(13) . "<br />" . chr(13);
}
else
{
echo "<b>Email to " . $EmailTo . " was not sent</b>" . chr(13) . "<br />" . chr(13);
}
?>
For sending mail with attachment using php mail().Try this code:
<?php
//If there is no error, send the email
if(isset($_POST['ur_submit_button_name'])) {
$EmailTo = "Me#here.com";
$EmailFrom = "You#There.com";
$EmailSubject = "The Email Subject";
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "ip.zip";//store that zip file in ur root directory
$attachment = chunk_split(base64_encode(file_get_contents('ip.zip')));
// 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."--";
// send message
if (mail($to, $subject, $body, $headers)) {
$mail_sent=true;
echo "mail sent";
} else {
$mail_sent=false;
echo "Error,Mail not sent";
}
}
?>
i think you have an error in your email content,
generate your email content with imap_mail_compose function:
mail('', 'message subject', '', imap_mail_compose(array header, array body));

Adding an attachment to this mail form

I have this simple php mail form. It is working and I can use it for making my forms, but I have a problem:
I want to add 2 or 3 attachments to this form. I tried a lot reading about mail at php.net, but I cannot do it by my self.
<?php
$name=$_POST['name'];
$email=$_POST['email'];
$address=$_POST['address'];
$phone=$_POST['phone'];
$fax=$_POST['fax'];
$mobile=$_POST['mobile'];
$subject=$_POST['subject'];
$website=$_POST['website'];
$message=$_POST['message'];
$fulltext = "
______________________________________________
|
| This Is $name Information:
|______________________________________________
| Name : $name
|______________________________________________
| E-Mail : $email
|______________________________________________
| Address : $address
|______________________________________________
| Phone : $phone
|______________________________________________
| FAX : $fax
|______________________________________________
| Mobile : $mobile
|______________________________________________
| Subject : $subject
|______________________________________________
| Website : $website
|______________________________________________
| Message : $message
|______________________________________________
";
$to = 'support#site.com';
$subject = 'Connect FORM <<';
$headers = 'From: contactform#site.com' . "\r\n" .
$message = $fulltext;
mail($to, $subject, $message, $headers);
echo 'file ersal shod';
?>
I improved code from http://ru.php.net/manual/ru/function.mail.php#105661 :
<?php
function multi_attach_mail($to, $subject, $message, $files, $sendermail){
// email fields: to, from, subject, and so on
$from = "Files attach <".$sendermail.">";
//$subject = date("d.M H:i")." F=".count($files);
$message .= "\n".count($files)." attachments";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}\n";
$fp = #fopen($files[$i],"rb");
$data = #fread($fp,filesize($files[$i]));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $sendermail;
$ok = #mail($to, $subject, $message, $headers, $returnpath);
if($ok){ return $i; } else { return 0; }
}
multi_attach_mail("to#somebody.dom",
"Subject of mail" ,
"Hello world!!!",
array($_SERVER["DOCUMENT_ROOT"] . "/first.file", // one file in root of your site
$_SERVER["DOCUMENT_ROOT"] . "/second.file" // another one
),
"from#someone");
?>