通过PHP发送电子邮件中的HTML?

Aba*_*dis 57 html php email send

如何使用PHP发送带有图片的HTML格式电子邮件?我希望有一个页面包含一些设置和HTML输出,通过电子邮件发送到一个地址.我该怎么办?主要问题是附加文件.我可以这样做吗?

Chr*_*ris 124

这很简单,将图像留在服务器上并将PHP + CSS发送给他们......

$to = 'bob@example.com';

$subject = 'Website Change Reqest';

$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

$message = '<p><strong>This is strong text</strong> while this is not.</p>';


mail($to, $subject, $message, $headers);
Run Code Online (Sandbox Code Playgroud)

正是这一行告诉邮件程序和收件人该电子邮件包含(希望)格式良好的HTML,它需要解释:

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
Run Code Online (Sandbox Code Playgroud)

这是我得到信息的链接..(链接......)

你需要安全......

  • @UmerHayat同意了,但是我们都必须从某个地方开始,即使它只是学习谷歌的东西...... (4认同)
  • **永远不要**使用`html`,`head`或`body`元素,否则你会在基于网络的电子邮件系统上造成绝对的混乱,这些系统实际上遵循(X)HTML标准.另外*如果*发送HTML电子邮件*从不*发送标题级别大于`h2`作为您的电子邮件主题(在适当的系统上)将在页面上的单个`h1`元素中.如果这没有意义,请记住Google试图将您的页面解释为只有一个主标题的报纸(例如"我们赢得了战争!")和许多较小的标题(例如"被骚扰的驼鹿保存的小猫"). (4认同)
  • 这部分中有一个拼写错误"$ message.=".之前未声明此变量. (4认同)

Sub*_*jit 17

您需要使用图像的绝对路径来编写html代码.绝对路径意味着您必须在服务器中上传图像,并且必须在src图像属性中提供这样的直接路径<img src="http://yourdomain.com/images/example.jpg">.

以下是您参考的PHP代码: - 摘自http://www.php.net/manual/en/function.mail.php

<?php
// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
  <p>Here are the birthdays upcoming in August!</p>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";


// Mail it
mail($to, $subject, $message, $headers);
?>
Run Code Online (Sandbox Code Playgroud)


Jal*_*tel 9

我有一个这个代码,它将完美运行我的网站

  public function forgotpassword($pass,$name,$to)
    {
        $body ="<table  width=100% border=0><tr><td>";
        $body .= "<img width=200 src='";
        $body .= $this->imageUrl();
        $body .="'></img></td><td style=position:absolute;left:350;top:60;><h2><font color = #346699>PMS Pvt Ltd.</font><h2></td></tr>";
        $body .='<tr><td colspan=2><br/><br/><br/><strong>Dear '.$name.',</strong></td></tr>';
        $body .= '<tr><td colspan=2><br/><font size=3>As per Your request we send Your Password.</font><br/><br/>Password is : <b>'.$pass.'</b></td></tr>';
        $body .= '<tr><td colspan=2><br/>If you have any questions, please feel free to contact us at:<br/><a href="mailto:support@pms.com" target="_blank">support@pms.com</a></td></tr>';
        $body .= '<tr><td colspan=2><br/><br/>Best regards,<br>The PMS Team.</td></tr></table>';
        $subject = "Forgot Password";
        $this->sendmail($body,$to,$subject);
    }
Run Code Online (Sandbox Code Playgroud)

邮件功能

   function sendmail($body,$to,$subject)
        {
            //require_once 'init.php';


            $from='testing@gmail.com';      
            $headersfrom='';
            $headersfrom .= 'MIME-Version: 1.0' . "\r\n";
            $headersfrom .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $headersfrom .= 'From: '.$from.' '. "\r\n";
            mail($to,$subject,$body,$headersfrom);
        }
Run Code Online (Sandbox Code Playgroud)

图像网址功能是用于如果你想改变你只有一个功能改变你的图像我有很多邮件功能像忘记密码创建用户那里我使用图像网址功能你可以直接设置路径.

function imageUrl()
    {
        return "http://".$_SERVER['SERVER_NAME'].substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/")+1)."images/capacity.jpg";
    }
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以通过 PHP 轻松发送带有 HTML 内容的电子邮件。使用以下脚本。

<?php
$to = 'user@example.com';
$subject = "Send HTML Email Using PHP";

$htmlContent = '
<html>
<body>
    <h1>Send HTML Email Using PHP</h1>
    <p>This is a HTMl email using PHP by CodexWorld</p>
</body>
</html>';

// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// Additional headers
$headers .= 'From: CodexWorld<info@codexworld.com>' . "\r\n";
$headers .= 'Cc: welcome@example.com' . "\r\n";
$headers .= 'Bcc: welcome2@example.com' . "\r\n";

// Send email
if(mail($to,$subject,$htmlContent,$headers)):
    $successMsg = 'Email has sent successfully.';
else:
    $errorMsg = 'Email sending fail.';
endif;
?>
Run Code Online (Sandbox Code Playgroud)

可以从这里找到源代码和现场演示 -使用 PHP 发送漂亮的 HTML 电子邮件


小智 5

发送html电子邮件与使用php发送普通电子邮件没什么不同.需要添加的是php mail()函数的header参数中的内容类型.这是一个例子.

<?php
    $to = "toEmail@domain.com";
    $subject = "HTML email";
    $message = "
    <html>
    <head>
    <title>HTML email</title>
    </head>
    <body>
    <p>A table as email</p>
    <table>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    </tr>
    <tr>
    <td>Fname</td>
    <td>Sname</td>
    </tr>
    </table>
    </body>
    </html>
    ";
    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\b";
    $headers .= 'From: name' . "\r\n";
    mail($to,$subject,$message,$headers);
?>
Run Code Online (Sandbox Code Playgroud)

您也可以在这里查看w3schools的更详细解释