AbyssalSwamp  ActivaUser
» Guest:  Register | Login | 会员列表

RSS subscription to this AbyssalSwamp  

Previous thread Next thread
     
Title: 如何使用PHP发送文本和HTML电子邮件  
 
Phoenix001





UID 305208
Digest 0
Points 0
Posts
码币MB 0 Code
黄金 0 Catty
钻石 0 Pellet
Permissions 10
Register 2019-5-27
Status offline
如何使用PHP发送文本和HTML电子邮件

如何使用PHP发送文本和HTML电子邮件

如果使用PHP构建的Web应用程序,则可以使用预定义的函数从脚本轻松发送电子邮件。
PHP Mail()函数是从代码级别发送电子邮件的最简单方法。
我们可以使用PHP中的内置邮件()函数轻松发送文本和HTML电子邮件。
通常,文本消息通过电子邮件发送到收件人。
如果要发送良好的格式化电子邮件,则需要将HTML内容添加到电子邮件的邮件正文中。
在本教程中,我们将展示如何使用PHP Mail()函数发送文本和HTML电子邮件。

使用PHP发送文本电子邮件

以下代码使用PHP从带有文本消息内容的脚本发送电子邮件。

  • 使用PHP Mail()函数并通过所需的参数。

  • 'to' - 收件人电子邮件地址。

  • 'subject' - 电子邮件的主题。

  • “message” - 要发送的消息。

  • “headers” - From,CC,BCC, Content-type标题信息。

$to = 'user@example.com';
$from = 'sender@email.com';
$fromName = 'Sender_Name';
 
$subject = "Send Text Email with PHP by onitroad";
 
$message = "First line of text Second line of text";
 
//Additional headers
$headers = 'From: '.$fromName.'<'.$from.'>';
 
//Send email
if(mail($to, $subject, $message, $headers)){
   echo 'Email has sent successfully.';
}else{
   echo 'Email sending failed.';
}

在PHP中发送HTML电子邮件

以下代码使用PHP从HTML消息内容发送来自脚本的电子邮件。

  • 使用PHP Mail()函数并提供所需的参数。

  • 'to' - 收件人电子邮件地址。

  • 'subject' - 电子邮件的主题。

  • “message” - 要发送的消息。

  • “headers” - 来自,CC,BCC,内容类型标题。

  • 对于发送HTML格式化电子邮件,Content-Type标题是强制性的。

  • 添加头用于添加,CC,BCC等。

  • '$htmlcontent'变量保存电子邮件HTML内容。

$to = 'user@example.com';
$from = 'sender@example.com';
$fromName = 'SenderName';
 
$subject = "Send HTML Email in PHP by onitroad";
 
$htmlContent = '
    <html>
    <head>
        <title>Welcome to onitroad</title>
    </head>
    <body>
        <h1>Thanks you for joining with us!</h1>
        <table cellspacing="0" style="border: 2px dashed #FB4314; width: 100%;">
            <tr>
                <th>Name:</th><td>onitroad</td>
            </tr>
            <tr style="background-color: #e0e0e0;">
                <th>Email:</th><td>contact@onitroad.com</td>
            </tr>
            <tr>
                <th>Website:</th><td><a href="http://www.onitroad.com">www.onitroad.com</a></td>
            </tr>
        </table>
    </body>
    </html>';
 
//Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . " ";
$headers .= "Content-type:text/html;charset=UTF-8" . " ";
 
//Additional headers
$headers .= 'From: '.$fromName.'<'.$from.'>' . " ";
$headers .= 'Cc: welcome@example.com' . " ";
$headers .= 'Bcc: welcome2@example.com' . " ";
 
//Send email
if(mail($to, $subject, $htmlContent, $headers)){
    echo 'Email has sent successfully.';
}else{
   echo 'Email sending failed.';
}

发送电子邮件与文件中包含HTML内容

如果要发送具有大HTML内容的电子邮件,则始终是在HTML文件中单独消息内容的好主意。
当我们想使用用于发送邮件的动态电子邮件模板时,此功能非常有用。

  • 将消息HTML内容放在HTML文件('email_template.html')中。

  • 使用file_get_contents()函数从文件中检索HTML内容。

//Get HTML contents from file
$htmlContent = file_get_contents("email_template.html");


2022-10-28 14:27#1
View profile  Blog  Send a short message  Top
 
Phoenix001





UID 305208
Digest 0
Points 0
Posts
码币MB 0 Code
黄金 0 Catty
钻石 0 Pellet
Permissions 10
Register 2019-5-27
Status offline
RE:如何使用PHP发送文本和HTML电子邮件

111.png

2022-10-28 14:29#2
View profile  Blog  Send a short message  Top
     


  Printable version | Recommend to a friend | Subscribe to topic | Favorite topic  


 


All times are GMT+8, and the current time is 2026-1-14 00:07 Clear informations ->sessions/cookies - Contact Us - CAFFZ - ZAKE