2015-02-06 83 views
-5

簡而言之,我試圖在用戶使用php提交聯繫表單時向用戶寫入消息。問題是,我試圖通過添加樣式元素(如粗體)來設置消息的樣式,並用<br>間隔某些行。它似乎適用。在php中添加html屬性

以下是代碼。

$message = "Welcome". $full_name . "to Company!" . "<br>". "<br>". "<b>". " Company is 
    the mailbox for the 21st century that evolves per your need. " . "</b>". "<br>". "<br>". " 
    Letting you interact with you mail by viewing and deciding what to do with it as it is received at a 
    Company location throughout North America. ". "<b>". "Please use the below information to send a mail/package to your selected address:". "</b>". "<br>". $full_name. "<br>". $address. "<br>". "To login to your dashboard, please visit the following link"."<br>". "<a href=''http://company.com/userinterfaceSelect.php'>Your Dashboard</a>" ; 

其中有<br>, <b>,和一個鏈接,其中似乎沒有適用。

更新;

<?php 
if(isset($_POST['submit'])){ 
    $to = $_POST['email'];// this is your Email address 
    $from = "[email protected]"; // this is the sender's Email address 
    $full_name = $_POST['fullName']; 
     $address = $_POST['address']; 

    $subject = "Welcome to company"; 
    $subject2 = "Copy: New Registration"; 
    $message2 = "New User". "<br>" .$full_name . "\n\n" ; 
    $message = "Welcome". $full_name . "to company!" . "<br>". "<br>". "<b>". " company is 
    the mailbox for the 21st century that evolves per your need. " . "</b>". "<br>". "<br>". " 
    Letting you interact with you mail by viewing and deciding what to do with it as it is received at a 
    Company location throughout North America. ". "<b>". "Please use the below information to send a mail/package to your selected address:". "</b>". "<br>". $full_name. "<br>". $address. "<br>". "To login to your dashboard, please visit the following link"."<br>". "<a href=''http://company/userinterfaceSelect.php'>Your Dashboard</a>" ; 

    $headers = "From:" . $from; 
    $headers2 = "From:" . $to; 
    mail($to,$subject,$message,$headers); 
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender 
echo "Your mail has been sent successfuly ! Thank you for your feedback"; 
    // You can also use header('Location: thank_you.php'); to redirect to another page. 
    } 
?> 
+2

輸出怎麼樣?在給用戶的電子郵件中?在頁面上?定義'似乎適用'。等等 – Utkanos 2015-02-06 14:15:15

+3

'在php'中''文體元素'''它確實似乎適用.' ...(͡°͜ʖ͡°) – 2015-02-06 14:16:19

+0

輸出到用戶的電子郵件 – Jon220 2015-02-06 14:19:40

回答

1

你非常接近它的工作。

您需要添加一個標題,表示內容是HTML:

Content-Type: text/html; charset=ISO-8859-1 

例如,你的頭可能形成如下:

$headers = "From: [email protected]\r\n"; 
$headers .= "Reply-To: [email protected]\r\n"; 
$headers .= "CC: [email protected]\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 

參見下面的參考資料細節。

參考文獻:

http://php.net/manual/en/function.mail.php

http://css-tricks.com/sending-nice-html-email-with-php/

+0

謝謝,但那麼我將如何風格的身體? – Jon220 2015-02-06 14:41:24

+1

這個答案是正確的,但我的建議是將HTML放在外部文件中,並在其中放置特定格式的變量(例如'## fullname ##')。然後,當您需要發送郵件時,將該文件加載到一個字符串中並用該值替換變量。我認爲它更清潔,這可以讓你集中在某處的HTML模板。如果需要的話,它也可以減輕翻譯過程。 – 2015-02-06 14:41:41

+0

我同意,OP應該使用模板方法甚至某種類型的PEAR包。正如他所說,他正在開始。 – 2015-02-06 15:09:38