サーバーのphpファイルにメールスクリプトがあります。メール スクリプトは、php の「mail」関数を使用し、ini_set でオプションを設定してからメールを送信します。
スクリプトは、本文/コンテンツおよびクエリ文字列を介してオプションをスクリプトに送信する http ポストを介して呼び出すことができます。
さて、私の質問は次のとおりです: ini_set でオプションを設定するメール php を同時に数回呼び出すのは安全ですか、それとも実行中のスクリプトの異なるインスタンスが互いのオプションを上書きしますか?
<?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";
}
?>
スクリプトは機能していますが、同時に複数回実行しようとすると壊れるかどうかはわかりません。
このスクリプトを使用する理由は、ホストがサーバー内からのメールの送信と無制限の数のみを許可しているためです。
おそらく、スクリプトを呼び出すコードを表示することもできます。これは C# でプログラミングされており、複数のスレッドで実行されます。ご覧のとおり、スクリプトへの呼び出しを「lock(emailLock)」で保護して、スクリプトへの呼び出しが同時に 1 つだけになるようにしています。何も台無しにせずにこのロックを外すことができるのだろうか
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");
}
}