2017-01-17 242 views
-1

我希望通過聯繫表單將電子郵件發送到我的電子郵件地址。在表單上,​​客戶將輸入他們的姓名,電子郵件地址和消息。但是,當電子郵件發送到我的地址時,我可以只能看到用戶的消息。 我該怎麼做才能讓客戶姓名,電子郵件地址和郵件顯示在電子郵件中。使用PHP發送電子郵件

if (empty($_POST) === false) { 
    $errors = array(); 

    $name  = $_POST['name']; 
    $email = $_POST['email']; 
    $message = $_POST['message']; 

    if (empty($name) === true || empty($email) === true || empty($message) === true) { 
     $errors [] = 'Name ,email and message are required !'; 
    } else { 
     if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { 
      $errors[] = 'That\'s not a valid email address'; 
     } 

     if (ctype_alpha ($name) === false) { 
      $errors[] = 'Name must only contain Letters'; 
     } 
    } 
    if (empty($errors) === true) { 
     mail('[email protected]', 'Contact form', $message, 'From: ' . $email); 
     header('Location: index.php?sent'); 
     exit(); 
    }  
} 

<!DOCTYPE html> 
<html> 
     <head> 
       <title>A contact form</title> 
     </head> 
     <body> 
      <?php 
      if (isset($_GET['sent']) === true) { 
       echo '<p>Thanks for Contacting us !</p>'; 
      } else { 
       if (empty($errors) === false) { 
        echo '<ul>'; 
        foreach ($errors as $error) { 
         echo '<li>', $error, '</li>' ; 
       } 
       echo'</ul>'; 
      } 
      ?> 
      <form action="" method="post">   
       <p> 
        <label for="name">Name:</label><br> 
         <input type="text" name="name" id="name" <?php if (isset($_POST['name']) === true) {echo 'value="', strip_tags($_POST['name']), '"'; } ?>> 
         </p> 
         <p> 
          <label for="email">Email:</label><br> 
          <input type="text" name="email" id="email" <?php if (isset($_POST['email']) === true) {echo 'value="', strip_tags($_POST['email']), '"'; } ?>> 
         </p> 
         <p> 
          <label for="message">Message</label><br> 
          <textarea name="message" id="message"><?php if (isset($_POST['message']) === true) {echo strip_tags($_POST['message']); } ?></textarea> 
         </p> 
         <p> 
          <input type="submit" value="Submit"> 
         </p> 
        </form> 
       <?php 
       } 
       ?> 
     </body> 
</html> 
+0

應如何顯示姓名,地址和信息?像「來自:Peter Parker [email protected]」?請更具體地說明你想實現的目標。 – elementzero23

+0

正確放入參數並將附加值附加到$ message字符串。有關發送信息請參閱http://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php – Blackbam

+0

首先閱讀'mail()'文檔 - http://php.net/manual /en/function.mail.php –

回答

0

只是名字和電子郵件添加到消息。我們一直使用這種方法使用PHP郵件。請參閱下面的變量$newMessage。它包含消息以及添加到其頂部的名稱和電子郵件以便於參考。

如果您創建$headers變量作爲數組或字符串,則取決於您將擁有多少附加參數,從而更容易看到您正在做什麼。我在這裏完成的方式會在電子郵件的「發件人」部分看到人員名稱。

<?php 

if (empty($_POST) === false) { 
$errors = array(); 

$name  = $_POST['name']; 
$email = $_POST['email']; 
$message = $_POST['message']; 

if (empty($name) === true || empty($email) === true || empty($message) === true) { 
    $errors [] = 'Name ,email and message are required !'; 
} else { 
    if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { 
     $errors[] = 'That\'s not a valid email address'; 
    } 

    if (ctype_alpha ($name) === false) { 
     $errors[] = 'Name must only contain Letters'; 
    } 

} 
if (empty($errors) === true) { 

    $headers = 'From: ' . $name . ' <' . $email . '>'; 
    // Create the new message to send with the name and email. 
    $newMessage = 'Name: ' . $name . '<br/>'; 
    $newMessage .= 'Email: ' . $email . '<br/>'; 
    $newMessage .= $message; 

    mail('[email protected]', 'Contact form', $newMessage, $headers); 

    header('Location: index.php?sent'); 
    exit(); 
} 

} 
?> 
0
$name  = $_POST['name']; 

$email = $_POST['email']; 

$message = $_POST['message']; 

$message = $message. " Email: $email". " Name: $name"; 

您可以編輯短信參數是更長的和包含更多的信息。你甚至可以將其設置爲html。

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

0

根據PHP mail()手冊:

to:接收器,或郵件的接收器。

該字符串的格式必須符合»RFC 2822。一些 例子是:

[email protected] 
    [email protected], [email protected] 
    User <[email protected]> 
    User <[email protected]>, Another User <[email protected]> 

mail()描述:

bool mail (string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]]) 

實施例1:

<?php 
mail("john doe <[email protected]>", "my subject", "my message") 
?> 

例2:

<?php 
$to  = 'john doe <[email protected]>'; 
$subject = 'the subject'; 
$message = 'hello'; 
$headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

mail($to, $subject, $message, $headers); 
?>