Hello,
I have a form and php script for processing giveaways in my site. I ask for Name, email, phone, city, age, sex and 5 email address of friends to refer to the site (this last one is optional). The collected data is saved on a text file (CSV) and the emails for refer are sent in the same moment. The script takes about 5 to 10 seconds to execute I think is due to the mail() used for sending the emails to the friends. I was wondering if there is a way to optimize this code and maybe find another way to make it load faster when sending the refer emails.
Here is the code:
<?php
function AppendFile($path,$value) {
$F = fopen($path,"a");
fwrite($F,$value."\n");
fclose($F);
}
if (!$Name || !$Email || !$Sex) {
//Required left in blank
echo "Please go back and fill all the required fields.";
}
else
{
$today = getdate();
$date = sprintf("%d/%d/%d %d:%d:%d",$today[mon],$today[mday],$today[year],$today[hours],$today[minutes],$today[seconds]);
// Start Refers //
// The variable 'Email' is the user email address. //
// And the variables 'Email1' through 'Email5' are the referred email address. //
$message = "Hi \n Visit this site http://www.pulsorock.com. I hope you like it. \n\n $Name";
if ($Email1) {
mail($Email1, "Hi! Visit this site", $message,
"From: $Email\r\n"
."X-Mailer: Boletin de Pulsorock.com");
}
if ($Email2) {
mail($Email2, "Hi! Visit this site", $message,
"From: $Email\r\n"
."X-Mailer: Boletin de Pulsorock.com");
}
if ($Email3) {
mail($Email3, "Hi! Visit this site", $message,
"From: $Email\r\n"
."X-Mailer: Boletin de Pulsorock.com");
}
if ($Email4) {
mail($Email4, "Hi! Visit this site", $message,
"From: $Email\r\n"
."X-Mailer: Boletin de Pulsorock.com");
}
if ($Email5) {
mail($Email5, "Hi! Visit this site", $message,
"From: $Email\r\n"
."X-Mailer: Boletin de Pulsorock.com");
}
// End Refers //
$Name = sprintf("\"%s\"",$Name);
$Email = sprintf("\"%s\"",$Email);
$Telephone = sprintf("\"%s\"",$Telephone);
$City = sprintf("\"%s\"",$City);
$Age = sprintf("\"%s\"",$Age);
$Sex = sprintf("\"%s\"",$Sex);
$Email1 = sprintf("\"%s\"",$Email1);
$Email2 = sprintf("\"%s\"",$Email2);
$Email3 = sprintf("\"%s\"",$Email3);
$Email4 = sprintf("\"%s\"",$Email4);
$Email5 = sprintf("\"%s\"",$Email5);
$sentdate = sprintf("\"%s\"",$date);
$d = ",";
$Output = $Name.$d.$Email.$d.$Telephone.$d.$City.$d.$Age.$d.$Sex.$d.$Email1.$d.$Email2.$d.$Email3.$d.$Email4.$d.$Email5.$d.$sentdate;
AppendFile($DOCUMENT_ROOT . "/file-name.csv",$Output);
// endif
}
echo "Thank you for your participation.";
?>