php
  • html
  • email
  • contact-form
  • 2015-07-19 56 views -3 likes 
    -3

    我在PHP的電子郵件的形式,我想一個主題添加到郵件正文:主題字段添加到PHP電子郵件的形式

    <?php 
        if (isset($_POST["submit"])) { 
         $name = $_POST['name']; 
         $email = $_POST['email']; 
         $message = $_POST['message']; 
         $from = 'Camino Contact Form'; 
         $to = '[email protected]'; 
         $subject = 'Message from Camino.bo '; 
         $body ="From: $name\n E-Mail: $email\n Message:\n $message"; 
    
    
    // If there are no errors, send the email 
    if (!$errName && !$errEmail && !$errMessage) { 
        if (mail ($to, $subject, $body, $from)) { 
         $result='<div class="success">Thank You! I will be in touch</div>'; 
        } else { 
         $result='<div class="danger">Sorry there was an error sending your message. Please try again.</div>'; 
        } 
    } 
        } 
    ?> 
    

    此表單提交的電子郵件的主題爲「寄語Camino.bo」,這是確定,但我想從下拉菜單中的電子郵件正文提交用戶選擇的值:

    HTML:

    <select name="subject" id="email_subject"> 
        <option value="default subject">Select a subject</option> 
        <option>Product A</option> 
        <option>Product B</option> 
        <option>Product C</option> 
    </select> 
    

    我怎樣才能做到這一點? PS:我的表單中的主題字段是可選的,不需要驗證。

    +0

    http://stackoverflow.com/a/6670067/2889187 –

    回答

    0

    我不知道如果我失去了一些東西,但在你的HTML,你要值添加到你的選擇剩餘:

    <select name="subject" id="email_subject"> 
        <option value="default subject">Select a subject</option> 
        <option value="Product A">Product A</option> 
        <option value="Product B">Product B</option> 
        <option value="Product C">Product C</option> 
    </select> 
    

    然後你的PHP可能如下:

    <?php 
    if (isset($_POST["submit"])) { 
        $name = $_POST['name']; 
        $email = $_POST['email']; 
        $message = $_POST['message']; 
        $from = 'Camino Contact Form <[email protected]>'; 
        $to = '[email protected]'; 
        $subject = 'Message from Camino.bo '; 
        $select_subject = $_POST['subject']; 
    
        $body ="From: $name\n E-Mail: $email\n Message: $message\n Subject: $select_subject"; 
    
        $headers = "From: " . $from; 
    
    
        // If there are no errors, send the email 
        if (!$errName && !$errEmail && !$errMessage) { 
         if (mail ($to, $subject, $body, $headers)) { 
          $result='<div class="success">Thank You! I will be in touch</div>'; 
         } else { 
          $result='<div class="danger">Sorry there was an error sending your message. Please try again.</div>'; 
         } 
        } 
    } 
    ?> 
    

    請注意,我已經添加了$ select_subject並將其添加到您的電子郵件$ body變量中。另外,請將'[email protected]'替換爲您希望使用的FROM電子郵件地址。也許你可以進一步闡述你的問題,但我認爲這是你想要的。請記住,這不是一個代碼寫作服務,所以你應該在你想獲得的確切的技術知識中更加具體,以便將來可以做到這一點。另外,我不知道你是否已經在你的代碼中的其他地方處理過這個問題,但是我認爲如果$ errName,$ errEmail或$ errMessage都計算爲true,你應該在屏幕上顯示一條消息。現在,如果其中任何一個爲真(假設您在此處粘貼的代碼已完成),則最終用戶在提交表單時屏幕上不會顯示任何內容,他們可能會對發生的事情感到困惑,並嘗試重新提交。

    1

    您的變量有點混淆。 mail()函數的第4個參數不是'From',而是'電子郵件標題',它可以是正確格式的任意數量的標題值,每個標題值都用一個新行分隔。在「從」只會是那些標題之一..

    $to = '[email protected]'; 
    
    $subject = 'My Email Subject'; 
    
    $body = 'The body text of your email....'; 
    
    $headers = "MIME-Version: 1.0\r\n". 
          "Content-type: text/html; charset=iso-8859-1\r\n". 
          "From: YOU <[email protected]>\r\n"; 
          // and others as required 
    
    mail($to, $subject, $body, $headers); 
    

    添加您的發貼選項的$body標籤的要求

    相關問題