当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > mail() 函数
The mail() function allows you to send emails directly from a script.
Mail()函数的作用是:直接从脚本发送邮件。
This function returns TRUE if the email was successfully accepted for delivery, otherwise it returns FALSE.
如果同意发送邮件,则返回True;否则返回False。
mail(to,subject,message,headers,parameters) |
Parameter 参数 | Description 描述 |
---|---|
to | Required. Specifies the receiver / receivers of the email 必要参数。指定邮件接收者的Email |
subject | Required. Specifies the subject of the email. Note: This parameter cannot contain any newline characters 必要参数。指定邮件主题。注意:这个参数不可以包含换行符[newline characters] |
message | Required. Defines the message to be sent. Each line should be separated with a LF (n). Lines should not exceed 70 characters. 必要参数。定义发送的邮件内容 Windows note: If a full stop is found on the beginning of a line in the message, it might be removed. To solve this problem, replace the full stop with a double dot: |
headers | Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (rn). 可选参数。指定附加的header,如:From、Cc以及Bcc;如要继续添加header,则应该用(rn)隔开 Note: When sending an email, it must contain a From header. This can be set with this parameter or in the php.ini file. |
parameters | Optional. Specifies an additional parameter to the sendmail program (the one defined in the sendmail_path configuration setting). (i.e. this can be used to set the envelope sender address when using sendmail with the -f sendmail option) 可选参数。为sendmail程序指定一个附加的参数(它将在sendmail_path属性设置中定义)。(例如:通过是用sendmail的“-f sendmail”选项,它可以设置envelope sender的路径) |
Note: Keep in mind that just because the email was accepted for delivery, it does NOT mean the email is actually sent and received.
注意:同意发送或接收email的并不意味着接收者已经真正收到了email。
Send a simple email:
发送一封简单的email:
<?php $txt = "First line of textnSecond line of text"; // Use wordwrap() if lines are longer than 70 characters $txt = wordwrap($txt,70); // Send email mail("[email protected]","My subject",$txt); ?> |
Send an email with extra headers:
发送一封包含额外header的email:
<?php $to = "[email protected]"; $subject = "My subject"; $txt = "Hello world!"; $headers = "From: [email protected]" . "rn" . "CC: [email protected]"; mail($to,$subject,$txt,$headers); ?> |
Send an HTML email:
发送一封HTML格式的email:
<?php $to = "[email protected], [email protected]"; $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> "; // Always set content-type when sending HTML email $headers = "MIME-Version: 1.0" . "rn"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "rn"; // More headers $headers .= 'From: <[email protected]>' . "rn"; $headers .= 'Cc: [email protected]' . "rn"; mail($to,$subject,$message,$headers); ?> |