Jump to content

Email script not always working


dom77

Recommended Posts

Hi,

 

I am relatively new to php and am having problems with an email script. The script receives payment verification from paypal and then sends out an email to the new customer with their login/password details. The problem is, the users arent always getting the emails (around 15 - 20% of them). Can someone have a look at the code and let me know what I am doing wrong?

 

thanks - code below:

 

 

 

<?php

 

$dbhost = 'myIp';

$dbuser = 'myUser';

$dbpass = 'myPass';

//connect to the database

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

//check connection was successful

if (!$conn) {

  echo( "<p>Unable to connect to the database server at this time.</p>" );

  exit();

}

//select the database

$dbname = 'myDbName';

mysql_select_db($dbname);

//check connection was successful

if (!mysql_select_db($dbname) ) {

  echo( "<p>Unable to locate the database at this time.</p>" );

  exit();

}

 

 

 

// 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.paypal.com', 443, $errno, $errstr, 30);

 

if (!$fp) {

// HTTP ERROR

} else {

fputs ($fp, $header . $req);

while (!feof($fp)) {

$res = fgets ($fp, 1024);

if (strcmp ($res, "VERIFIED") == 0) {

 

// PAYMENT VALIDATED & VERIFIED!

 

$email = $_POST['payer_email'];

$password = mt_rand(1000, 9999);

$date = date("Y-m-d");

 

//check to see if the email exists in the database

$exists = mysql_query("SELECT * FROM members WHERE Email = '$email'");

//if so, setup the new account

if (mysql_num_rows($exists) < 1) {

 

  //insert data into database

  $insert = mysql_query("INSERT INTO members (Password,Email,SignUpDate) VALUES ('$password','$email','$date')");

 

  //save email variables

  $to      = $email;

  $subject = 'MyCompany';

  $from = 'From:myEmail' . "\r\n";

 

  $body = '

   

  Thank you for your purchase.

 

  Your account information

  -------------------------

  Email: '.$email.'

  Password: '.$password.'

  -------------------------

               

  You can now sign in using the login box: http://www.blah.com

 

  Note: Once logged in you will have the option to change your password.';

 

  //send the confirmation email

  ini_set('sendmail_from', $from);

  $headers =

        "From: {$from} <{$from}>\n".

        "Reply-To: {$from} <{$from}>\n".

        "Content-type: text/plain; charset=UTF-8";

  return mail("{$to} <{$to}>", $subject, $body, $from, "-f".$from);

} else {

  //Otherwise member is purchasing more credits so add them to the account

  mysql_query("UPDATE members SET Credits = Credits+100 WHERE Email = '$email'");

}

 

 

}

 

else if (strcmp ($res, "INVALID") == 0) {

 

// PAYMENT INVALID & INVESTIGATE MANUALY!

 

$to      = '[email protected]';

$subject = 'Download Area | Invalid Payment';

$body = 'Dear Administrator,A payment has been made but is flagged as INVALID.Please verify the payment manualy and contact the buyer.Buyer Email: '.$email.'';

$from = 'From:[email protected]' . "\r\n";

 

//send the email

ini_set('sendmail_from', $from);

$headers =

      "From: {$from} <{$from}>\n".

      "Reply-To: {$from} <{$from}>\n".

      "Content-type: text/plain; charset=UTF-8";

return mail("{$to} <{$to}>", $subject, $body, $headers, "-f".$from);

 

}

}

fclose ($fp);

}

?>

Link to comment
https://forums.phpfreaks.com/topic/192922-email-script-not-always-working/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.