2017-06-26 3 views
0

Ich bin ein Begniner in PHP. Ich habe eine Aufgabe, die E-Mails von der Website sendet, bitte helfen Sie mir. Ich habe HTML und PHP-Code angehängt, die ich ausprobiert habe. Das funktioniert nicht genau. Das Problem iam konfrontiert ist es nie zeigt von wo die Post kommt. Stattdessen zeigt es den Hostnamen. Voraussetzung: Sub: E-Mail-ID:Mail senden von einer Website mit PHP

Nachricht

HTML CODE

<form class="form-horizontal" method="post" name="myemailform" action="form-to-email.php" > 
    <div class="row"> 
     <div class="col-lg-6"> 
      <div class="form-group" style="padding-right:10px;"> 
       <label>Name</label> 
       <input type="text" class="form-control" id="name" name="name" placeholder="NAME..." value="">          
      </div> 
     </div> 
     <div class="col-lg-6"> 
      <div class="form-group"> 
       <label for="exampleInputEmail1">Email</label> 
        <input type="email" class="form-control" id="email" name="email" placeholder="EMAIL..." value="">  
      </div> 
     </div> 
    </div> 
    <div class="row"> 
     <div class="col-lg-6"> 
      <div class="form-group" style="padding-right:10px;"> 
       <label>Telephone</label> 
        <input type="email" class="form-control" id="telephone"> 
      </div> 
     </div> 
     <div class="col-lg-6"> 
      <div class="form-group"> 
       <label>Mobile</label> 
       <input type="email" class="form-control" id="mobile"> 
      </div> 
     </div> 
    </div> 
    <div class="row"> 
     <div class="col-lg-12"> 
      <div class="form-group"> 
       <label>Message</label> 
       <textarea class="form-control" rows="4" id="message" name="message" placeholder="MESSAGE..."></textarea> 
      </div> 
     </div> 
    </div> 
    <button type="submit" class="btn btn-default sub">Send</button> 
</form> 

PHP CODE

<?php 
if(!isset($_POST['submit'])) 
{ 
    //This page should not be accessed directly. Need to submit the form. 
    echo "error; you need to submit the form!"; 
} 
$name = $_POST['name']; 
$visitor_email = $_POST['email']; 
$message = $_POST['message']; 
$telephone = $_POST['telephone']; 
$mobile = $_POST['mobile']; 
$subject = $_POST['subject']; 

//Validate first 
if(empty($name)||empty($visitor_email)||empty($message)) 
    { 
     echo "Name,email and message are mandatory!"; 
     exit; 
    } 

if(IsInjected($visitor_email)) 
{ 
    echo "Bad email value!"; 
    exit; 
} 

$email_from = $visitor_email;//<== update the email address 
$email_body = "Name:\t $name.\n". 
       "Email:\t $vi\nsitor_email.\n". 
       "Telephone:\t $telephone.\n". 
       "Mobile:\t $mobile.\n". 
       "Message:\t $message.\n". 

$to = "[email protected]";//<== update the email address 
//Send the email! 
mail($to,$subject,$email_body); 
//done. redirect to thank-you page. 
header('Location: thank-you.html'); 


// Function to validate against any email injection attempts 
function IsInjected($str) 
{ 
    $injections = array('(\n+)', 
      '(\r+)', 
      '(\t+)', 
      '(%0A+)', 
      '(%0D+)', 
      '(%08+)', 
      '(%09+)' 
     ); 
    $inject = join('|', $injections); 
    $inject = "/$inject/i"; 
    if(preg_match($inject,$str)) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

>

+0

verwenden Sie einen kopierten Code? Oder hast du das geschrieben? – ad08

+1

Im Aufruf von mail(), von dem Sie nie hinzugefügt haben – derHugo

Antwort

0

individuelle header in Mail-fu Verwenden nction.Check unten Funktionscode mail:

<?php 
$to = "[email protected], [email protected]"; 
$subject = "HTML email"; 

$message = " 
<html> 
<head> 
<title>HTML email</title> 
</head> 
<body> 
<p>This email contains HTML Tags!</p> 
<table> 
<tr> 
<th>Firstname</th> 
<th>Lastname</th> 
</tr> 
<tr> 
<td>John</td> 
<td>Doe</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\n"; 

$headers .= 'From: <[email protected]>' . "\r\n"; 
$headers .= 'Cc: [email protected]' . "\r\n"; 

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

https://www.w3schools.com/php/func_mail_mail.asp