PHPメール()

WBOY
リリース: 2024-08-29 13:08:50
オリジナル
343 人が閲覧しました

mail() 関数は、スクリプトから簡単にメールを直接送信できるようにするのに役立ちます。通常は 5 つのパラメータを受け入れますが、そのうちの最初の 3 つだけが必須であり、他の 2 つのパラメータはオプションであり、すべてのパラメータが mail() 関数の括弧内に記載されている場合は必須ではありません。これは、必要に応じていつでも何らかのフォームなどを使用して Web サイトやブログからメールを直接送信するのに役立ちます。これは、PHP スクリプトからの電子メールの送信に役立つ PHP プログラミング言語の組み込み関数です。重要なタスクやイベントについてユーザーに通知する場合、費用対効果が高くなります。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文とパラメータ

以下は構文です

mail(to1, subject1, message1, headers1, parameters1);
ログイン後にコピー

mail() 関数のパラメータの説明:

  • mail() 関数の to1 パラメータ: PHP プログラミング言語の mail() 関数の「to1」パラメータは必須フィールドです。メールの受信者を指定するのに役立ちます。
  • mail() 関数の subject1 パラメータ: PHP プログラミング言語の mail() 関数の「subject1」パラメータも必須フィールドです。メールの件名を指定するのに役立ちます。このパラメータには、改行文字が含まれる場合と含まれない場合があります。
  • mail() 関数の message1 パラメータ: PHP プログラミング言語の mail() 関数の「message1」パラメータも、上記 2 つのパラメータと同様に必須フィールドです。送信されるメッセージを定義するのに役立ちます。その各行は LF (n) で区切る必要があります。各行は 70 文字を超えてはなりません。 Windows では、メッセージ内の特定の行の先頭に FULLSTOP が見つかった場合、それが削除される可能性があります。この種の問題を解決するには、終点文字を二重ドット文字「..」に置き換える必要があります。
  • mail() 関数の headers1 パラメータ: PHP 言語の headers1 パラメータはオプションのフィールドですが、Cc、From、Bcc 関数などの追加ヘッダーを指定するのに役立ちます。追加ヘッダーの一部は CRLF – (rn) で区切られます。 EMAIL の送信処理中に、From ヘッダーが含まれる場合があります。これは、この headers1 パラメータまたは PHP.INI 形式ファイルで設定できます。
  • mail() 関数のparameters1 パラメータ: mail 関数のparameters1 パラメータはオプションです。このパラメータは追加パラメータを指定します。これは、電子メール送信プログラムに追加パラメータを指定するのに役立ちます。この設定は、–f send mail オプションを使用するときにエンベロープ送信者のアドレスに設定されます。

PHP では mail() 関数はどのように動作しますか?

PHP プログラミング言語の mail() 関数は、PHP スクリプト自体から直接メールを送信することを支援するだけで機能します。 PHP 4 以降のバージョンで動作します。 PHP 4 バージョンより前のバージョンでは、この mail() は機能しないため、PHP バージョンが PHP 4 バージョンより前の場合は mail() 関数を試行しないでください。これは、アドレスパラメータのハッシュ値を返すことによって機能します。ただし、メールの配信が受け入れられたとしても、そのメールが実際に受信または送信されたことを意味するわけではないことに注意する必要があります。

PHP 4.0.5 バージョンには、parameter1 パラメーターが追加されています。 PHP 4.2.3 の PHP バージョンでは、セーフ モードでは、parameter1 パラメーターが無効になります。 Windows OS 上の PHP 4.3.0 バージョンの場合Cc、Bcc、From、Date などのカスタム ヘッダーを受け入れますが、大文字と小文字は区別されません。 PHP 5.4 バージョンでは、ヘッダー パラメーターの目的のみにヘッダー インジェクション保護が追加されています。 PHP 7.2 バージョンでは、headers1 パラメータも配列を受け入れるようになります。

以下に挙げる例を示します:

例 #1

この例では、PHP プログラミング言語の mail() 関数を実装します。ここでは、最初に PHP タグが作成され、その中にいくつかの値を含むいくつかの変数が作成されます。最初に、メールの送信に必要な特定のメール/Gmail アドレスである文字列値を使用して「to_email1」が作成されます。次に、件名として表示する文字列コンテンツを含む「subject1」変数が作成され、次にメッセージ変数が作成され、重要なメッセージ テキスト情報/その他が割り当てられます。 to_email1アドレスの人に情報を指定することです。次に、header1 変数が作成されます。次に、メインの PHP 関数 mail() が使用されます。ここでは、mail() 関数内に 4 つのパラメータが含まれています。この関数は、PHP スクリプト自体を使用してメールを送信する場合に役立ちます。

コード:

<?php
$to_email1 = 'name1 @ company .com';
$subject1 = 'Testing the PHP Mail';
$message1 = 'This mail is sent using the PHP mail function';
$headers1 = 'From: noreply @ company .com';
mail($to_email1,$subject1,$message1,$headers1);
?>
ログイン後にコピー

出力:

PHPメール()

Example #2

This is the example of implementing the custom functions which helps in sending the secure mail to a specific person only with the help of the mail() function inside the PHP script. Here at first a function with some fields are created along with the condition statements. Then “to_email1” variable is created with some email address. Then subject1, messages, and headers1 variables are created. Based on the IF ELSE conditions and the mail() function along with the HTML display elements the program will run and the mail will be sent to the specified email person or other but the PHP.INI file settings should be perfect or use any online compiler if possible.

Code:

<?php
functionsanitize_my_email($field1) {
$field1 = filter_var($field1, FILTER_SANITIZE_EMAIL);
if (filter_var($field1, FILTER_VALIDATE_EMAIL)) {
return true;
} else {
return false;
}
}
$to_email1 = 'name @ company .com';
$subject1 = 'Testing PHP Mail';
$message1 = 'This mail is sent using the PHP mail ';
$headers1 = 'From: noreply @ company. com';
//check whether the email address is invalid or not $secure_check
$secure_check1 = sanitize_my_email($to_email1);
if ($secure_check1 == false) {
echo "Invalid input";
} else { //send email
mail($to_email1, $subject1, $message1, $headers1);
echo "This email is now sent using the PHP Mail";
}
?>
ログイン後にコピー

Output:

PHPメール()

Example #3

This is the example of implementing the PHP mail() function without using the header1 parameter inside of the mail() function of the PHP Programming Language. It is a simple mail but the header1 parameter is not used.

Code:

<?php
$to1 = '[email&#160;protected]';
$subject1 = 'The Marriage Proposal';
$message1 = 'Hi Radha, will you marry me? Say Yes or No';
$from1 = '[email&#160;protected]';
// Sending email
if(mail($to1, $subject1, $message1)){
echo 'Now Your mail will be sent automatically and successfully.';
} else{
echo 'Now it is Unable to send the email/gmail. Please try again.';
}
?>
ログイン後にコピー

Output:

PHPメール()

Example #4

This is the example of implementing the PHP mail() function in order to send an attachment file along with the mail. Here at first HTML form is created for some variable values and to get the attachment file. After clicking the submit button the PHP mail() function works with the help of the PHP mail function code. Then the output text will be displayed as shown in the output section below.

HTML File Code:

<html>
<body>
<form enctype="multipart/form-data" method="POST" action="string22.php">
<label>Your Name1 <input type="text" name="sender_name1" /></label><br>
<label>Your Email1 <input type="email" name="sender_email1" /></label><br>
<label>Subject1 <input type="text" name="subject1" /></label><br>
<label>Message1 <textarea name="message1"></textarea></label><br>
<label>Attachment1 <input type="file" name="attachment1" /></label><br>
<label><input type="submit" name="button" value="Submit" /></label><br>
</form>
</body>
</html>
ログイン後にコピー

PHP File Code:

<?php
if($_POST['button'] &&isset($_FILES['attachment1']))
{
$from_email         = '[email&#160;protected]'; //from mail, sender email addrress
$recipient_email    = '[email&#160;protected]'; //recipient email addrress
//Load POST data from HTML form
$sender_name1    = $_POST["sender_name1"] //sender name1
$reply_to_email = $_POST["sender_email1"] //sender email1, it will be used in "reply-to" header
$subject1        = $_POST["subject1"] //subject1 for the email
$message1        = $_POST["message1"] //message1 body1 of the email
//Get uploaded file data using $_FILES array
$tmp_name    = $_FILES['my_file']['tmp_name']; // Now get the temporary file name1 of the file on the server
$name        = $_FILES['my_file']['name'];  // Now get the name of the file1
$size        = $_FILES['my_file']['size'];  // Now get size of the file1 for size validation
$type        = $_FILES['my_file']['type'];  // Now get type of the file1
$error       = $_FILES['my_file']['error']; // Now get the error (if any)
//It is a validating form field which helps in attaching the file
if($file_error> 0)
{
die('Upload error or No files uploaded');
}
//It is like reading from a specific uploaded file and also the base64_encode content1
$handle1 = fopen($tmp_name, "r");  // Now setting the file handle1 only for reading the file
$content1 = fread($handle1, $size); // Now reading the file
fclose($handle1);                  // Now close upon completion
$encoded_content11 = chunk_split(base64_encode($content1));
$boundary1 = md5("random"); // Now defining the boundary1 with a md5 hashed value
//Now header
$headers1 = "MIME-Version: 1.0\r\n"; // Now Defining the MIME version
$headers1 .= "From:".$from_email."\r\n"; // Now the Sender Email
$headers1 .= "Reply-To: ".$reply_to_email."\r\n"; // Now Email addrress to reach back
$headers1 .= "content1-Type: multipart/mixed;\r\n"; // Now Defining content1-Type
$headers1 .= "boundary1 = $boundary1\r\n"; // Now Defining the boundary1
//Now this is about plain text
$body1 = "--$boundary1\r\n";
$body1 .= "content1-Type: text/plain; charset=ISO-8859-1\r\n";
$body1 .= "content1-Transfer-Encoding: base64\r\n\r\n";
$body1 .=chunk_split(base64_encode($message1));
//NOw attachment1
$body1 .= "--$boundary1\r\n";
$body1 .="content1-Type: $file_type; name=".$file_name."\r\n";
$body1 .="content1-Disposition: attachment1; filename=".$file_name."\r\n";
$body1 .="content1-Transfer-Encoding: base64\r\n";
$body1 .="X-attachment1-Id: ".rand(1000, 99999)."\r\n\r\n";
$body1 .= $encoded_content11; // NOw Attaching the encoded file with email
$sentMailResult1 = mail($recipient_email, $subject1, $body1, $headers1);
if($sentMailResult1 )
{
echo "This mail is sent using the PHP mail function"; // Now it will be printed only if the file is going to be sent successfully
unlink($name); // Now deleting the file just after the attachment1 is sent.
}
else
{
die("Sorry attachment mail not sent");
}
}
?>
ログイン後にコピー

Output of HTML Code:

PHPメール()

Output of PHP Code:

PHPメール()

Conclusion

We hope you learned what is the definition of PHP mail() function along with its syntax and explanation of the parameter of the mail() function, How the mail() function works in PHP along with various examples to under mail() concept better and so easily.

以上がPHPメール()の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
php
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!