Question

I have a mail script in a php file at my server. The mail script uses the php "mail" function and sets options with ini_set then sends a mail.

The script is callable via a http post that sends into the script the options via the body/content and query string.

Now, my question is this: Is it safe for me to call the mail php, which sets options with ini_set, several times at the same time or will the different instances of the running script overwrite each others options?

<?php
parse_str($_SERVER['QUERY_STRING']);

// $to is filled in by parse_str taking it from the query string
// $subject is filled in by parse_str taking it from the query string

$message = file_get_contents('php://input'); // $message is taken from the body/contents

$from = "from@mail.com";
$headers = "From:" . $from . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
ini_set( "sendmail_from", "from@mail.com" ); 
ini_set( "SMTP", "theHost" );  
ini_set( "smtp_port", "25" );
ini_set("username","uname"); 
ini_set("password","pwd");

if(mail($to,$subject,$message,$headers) == true){
    echo "1";
}else{
    echo "0";
}
?>

The script is working but I am not sure if it might break if I try to run it several times simultaneously.

The reason why I have this script is because my host only allows me to send and infinite number of emails from within the server.

Perhaps I could even show the code that I call the script with. It's programmed in C# and runs in several threads. As you can see I have the call to the script protected by a "lock(emailLock)" to make sure that there is only one call to the script at the same time. I wonder if it is possible to remove this lock without anything screwing up

string URI = String.Format("http://addressToThePhpScript.php",
    HttpUtility.UrlEncode("to@email.com"),
    HttpUtility.UrlEncode("The subject"));

using (WebClient wc = new WebClient()){
    wc.Headers[HttpRequestHeader.ContentType] = "text/plain";

    string response;

    lock(emailLock){
        response = wc.UploadString(URI, body.ToString());
    }

    if (response == "1"){
        //Success
    }else{
        throw new Exception("Email could not be sent");
    }
}
Was it helpful?

Solution

After running some tests I have concluded that it indeed should be safe to use set_ini in several requests at the same time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top