Jump to content

php 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 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      = 'myEmail@blah.com';

$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:noreply@yourmy.com' . "\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
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.