首頁  >  文章  >  後端開發  >  PHP 郵件()

PHP 郵件()

WBOY
WBOY原創
2024-08-29 13:08:50532瀏覽

mail() 函數有助於輕鬆地從腳本直接發送郵件。它通常接受五個參數,但只有前三個參數是強制性的,如果所有參數都在 mail() 函數的括號內提及,則其他兩個參數是可選的且不是強制性的。它有助於在需要時使用某種形式或其他形式直接從網站或部落格發送郵件。它是 PHP 程式語言的內建函數,有助於從 PHP 腳本發送一些電子郵件。在通知用戶重要任務或事件時,它具有成本效益。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

語法與參數

以下是文法

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) 分隔。當我們發送電子郵件時,它可能包含一些 From 標頭。這可以使用 headers1 參數或 PHP.INI 格式檔案來設定。
  • mail() 函數的parameters1 參數:mail 函數的parameters1 參數是可選參數。此參數指定附加參數。它有助於指定電子郵件發送程式的附加參數。當我們使用 –f 發送郵件選項時,此設定將被設定為信封寄件者地址。

mail() 函數在 PHP 中如何運作?

PHP 程式語言的 mail() 函數的工作原理只是幫助直接從 PHP 腳本本身發送郵件。它適用於 PHP 4+ 版本。在 PHP 4 版本之前,此 mail() 不起作用,因此如果您的 PHP 版本在 PHP 4 版本之前,請不要嘗試 mail() 函數。它的工作原理是傳回位址參數的雜湊值。但我們必須記住,即使郵件已被接受投遞,但這並不意味著電子郵件已實際收到或發送。

PHP 4.0.5版本新增parameter1參數。在 PHP 4.2.3 PHP 版本中,parameter1 參數在安全模式下被停用。 Windows作業系統下的PHP 4.3.0版本接受一些自訂標頭,例如副本、密件副本、寄件者、日期,且它們不區分大小寫。在 PHP 5.4 版本中,僅出於 headers 參數的目的添加了 header 注入保護。在 PHP 7.2 版本中, headers1 參數也將接受一個陣列。

範例

以下是提到的範例:

範例#1

在此範例中,我們將實作 PHP 程式語言的 mail() 函數。這裡首先創建 PHP 標籤,並在其中創建一些變量,其中包含一些值。首先,使用一些字串值來建立“to_email1”,該字串值是發送郵件所需的特定電子郵件/gmail 位址。然後使用一些字串內容建立“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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP 中的驗證下一篇:PHP 中的驗證