dom77 Posted February 20, 2010 Share Posted February 20, 2010 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 10% of them). It could be that the email is going in their junk mail or, it could be a probelm with the script. Can someone have a look at the code and let me know if I am doing anything 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/192727-php-email-script-not-always-working/ Share on other sites More sharing options...
rbrown Posted February 25, 2010 Share Posted February 25, 2010 off hand I would send a duplicate email to another one of your accounts off your server to see if they are all going or not. But if it works once it should work all the time. One thing I would check... normally even if your sendmail program is down, it will que on a second mail server. So possibly they mail server has issues or there is no secondary server. Then once qued the mail server will try and deliver it for x amount of days depending on what the mail server is set for, then bounce it back to you (if they have it set that way) So I would first, make sure you get all the copies of all the mails then if not start investigating the mailserver setup. Bob Quote Link to comment https://forums.phpfreaks.com/topic/192727-php-email-script-not-always-working/#findComment-1017805 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.