Fungsi mail() membantu dalam membenarkan mel dihantar terus hanya dari skrip dengan begitu mudah. Ia menerima lima parameter biasanya tetapi hanya tiga yang pertama daripadanya adalah wajib dan dua lagi parameter adalah pilihan dan tidak wajib jika semua parameter disebut di dalam kurungan fungsi mel(). Ia membantu dalam menghantar mel terus dari tapak web atau blog apabila diperlukan menggunakan beberapa bentuk atau lain-lain. Ia adalah fungsi terbina dalam Bahasa Pengaturcaraan PHP yang membantu dalam menghantar beberapa e-mel daripada skrip atau skrip PHP. Ia menjimatkan kos apabila memberitahu pengguna tentang tugas atau acara penting.
Mulakan Kursus Pembangunan Perisian Percuma Anda
Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain
Di bawah ialah sintaks
mail(to1, subject1, message1, headers1, parameters1);
Penjelasan parameter fungsi mail():
Fungsi mail() Bahasa Pengaturcaraan PHP berfungsi hanya dengan membantu menghantar mel daripada skrip PHP itu sendiri secara langsung. Ia berfungsi untuk versi PHP 4+. Sebelum versi PHP 4, mail() ini tidak berfungsi jadi jangan cuba fungsi mail() jika versi PHP anda sebelum versi PHP 4. Ia berfungsi dengan mengembalikan nilai cincang parameter alamat. Tetapi kita harus ingat bahawa walaupun mel diterima untuk penghantaran tetapi ini tidak bermakna e-mel itu benar-benar diterima atau dihantar.
Versi PHP 4.0.5 ditambah dengan parameter parameter1. Dalam versi PHP 4.2.3 PHP, parameter parameter1 dilumpuhkan dalam mod selamat. Dalam versi PHP 4.3.0 di bawah Windows O.S. menerima beberapa pengepala tersuai seperti Sk, Skt, Daripada, Tarikh dan ia tidak peka huruf besar-kecil. Dalam versi PHP 5.4, perlindungan suntikan pengepala ditambah hanya untuk tujuan parameter pengepala. Dalam versi PHP 7.2, parameter headers1 juga akan menerima tatasusunan.
Di bawah adalah contoh yang dinyatakan:
Di sini dalam contoh ini, kami akan melaksanakan fungsi mel() Bahasa Pengaturcaraan PHP. Di sini pada mulanya teg PHP dicipta dan di dalamnya beberapa pembolehubah dicipta dengan beberapa nilai di dalamnya. Pada mulanya “to_email1” dicipta dengan beberapa nilai rentetan yang merupakan alamat e-mel/gmail yang khusus yang diperlukan untuk menghantar mel. Kemudian pembolehubah "subject1" dicipta dengan beberapa kandungan rentetan untuk dipaparkan sebagai subjek dan kemudian pembolehubah mesej dicipta dan kemudian ia diberikan dengan beberapa maklumat teks mesej penting/lain-lain. Ia adalah untuk menentukan maklumat kepada orang alamat to_email1. Kemudian pembolehubah header1 dicipta. Kemudian fungsi PHP utama mel() digunakan. Di sini 4 parameter disertakan di dalam fungsi mel(). Fungsi ini akan membantu dalam menghantar mel dengan skrip PHP itu sendiri.
Kod:
<?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); ?>
Output:
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:
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 protected]'; $subject1 = 'The Marriage Proposal'; $message1 = 'Hi Radha, will you marry me? Say Yes or No'; $from1 = '[email 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:
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 protected]'; //from mail, sender email addrress $recipient_email = '[email 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:
Output of PHP Code:
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.
Atas ialah kandungan terperinci mel PHP(). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!