Home > Backend Development > PHP Tutorial > php: Detailed explanation of mail() function usage and configuration usage

php: Detailed explanation of mail() function usage and configuration usage

伊谢尔伦
Release: 2023-03-11 15:30:02
Original
3068 people have browsed it

Configuration

If a worker wants to do his job well, he must first sharpen his tools. First, let's take Windows as an example to explain how to configure local mail.

Download attachment sendmail.zip
- Unzip to any path, modify sendmail.ini, and modify the following information according to actual needs.

  [sendmail]
   smtp_server=smtp.qq.com
   smtp_port=25
   
error_log
file=error.log
   debug_logfile=debug.log
   auth_username=***@qq.com
   auth_password=***
   force_sender=***@qq.com
 -php.ini
  [mail function]
   SMTP = smtp.qq.com
   smtp_port = 25
   sendmail_from = ***@qq.com
   sendmail_path = "D:/sendmail/sendmail.exe -t -i"
   mail.add_x_header = On
Copy after login

Note:
The current test is only successful sending of qq. The failure of 163 may be that he has a filtering system and can successfully send to gmail.

Syntax

mail(to,subject,message,headers,parameters)
Copy after login

Definition and usage

mail() FunctionAllows you to send an email directly from a script.
Returns true if the mail delivery is successfully received, otherwise returns false.
Explanation
In the message specified by the message parameter, lines must be separated by an LF (\n). Each line cannot exceed 70 characters.
(Under Windows) When PHP connects directly to an SMTP server, if a period is found at the beginning of a line, it will be deleted. To avoid this problem, replace the single period with two periods.

<?php  
$text = str_replace("\n.", "\n..", $text);  
?>
Copy after login

Tips and Notes

Note: You need to keep in mind that just because a mail delivery is accepted, it does not mean that the mail has reached its intended destination.
Example
The following quotesan official example of sending HTML email.

<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// 当发送 HTML 电子邮件时,请始终设置 content-type
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=utf-8" . "\r\n";
// 更多报头
$headers .= &#39;From: <webmaster@example.com>&#39; . "\r\n";
$headers .= &#39;Cc: myboss@example.com&#39; . "\r\n";
mail($to,$subject,$message,$headers);
?>
Copy after login


The above is the detailed content of php: Detailed explanation of mail() function usage and configuration usage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template