簡體   English   中英

通過 PHP 在電子郵件中發送 HTML

[英]Send HTML in email via PHP

如何使用 PHP 發送帶有圖片的 HTML 格式的電子郵件?

我想要一個包含一些設置和 HTML 輸出的頁面,該頁面通過電子郵件發送到某個地址。 我該怎么辦?

主要問題是附加文件。 我怎樣才能做到這一點?

很簡單,將圖像留在服務器上,然后將 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);

正是這一行告訴郵件發送者和收件人電子郵件包含(希望)它需要解釋的格式良好的 HTML:

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

這是我得到信息的鏈接..(鏈接...

不過你需要安全...

您需要使用圖像的絕對路徑對 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);
?>

我有這個代碼,它可以完美地運行在我的網站上

  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);
    }

郵件功能

   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);
        }

圖像 url 函數用於如果你想更改你只更改一個函數的圖像,我有很多郵件功能,比如忘記密碼在那里創建用戶,因為我正在使用圖像 url 函數,你可以直接設置路徑。

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

發送 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);
?>

您也可以在此處查看 w3schools 的更詳細解釋

您可以通過 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;
?>

可以從這里找到源代碼和現場演示 - 使用 PHP 發送漂亮的 HTML 電子郵件

最簡單的方法可能是只使用 Zend Framework 或任何其他框架,如 CakePHP 或 Symphony。

您也可以使用標准mail功能完成此操作,但您需要更多有關如何附加圖片的知識。

或者,只需將圖像托管在服務器上,而不是附加它們。 發送 HTML 郵件記錄在mail功能文檔中。

使用 PHPMailer,

要發送 HTML 郵件,您必須僅設置 $mail->isHTML(),並且您可以使用 HTML 標簽設置您的正文

這是一個寫得很好的教程:

rohitashv.wordpress.com/2013/01/19/how-to-send-mail-using-php/

訣竅是在構建 html body 部分時知道 Image mime 部分的內容 id。 歸結為制作 img 標簽

https://github.com/horde/horde/blob/master/kronolith/lib/Kronolith.php

查看函數 buildMimeMessage 以獲得一個工作示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM