PHP 邮件()

WBOY
发布: 2024-08-29 13:08:50
原创
531 人浏览过

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中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!