2016-05-25 9 views
0

Ich habe ein einfaches Formular auf meiner Seite erstellt und jetzt habe ich versucht, PHP-Skript zum Senden von E-Mails hinzuzufügen. Geht leider nicht. Nach dem Klicken auf die Schaltfläche möchte ich, dass der Benutzer ohne Umleitung auf meiner Seite bleibt.PHP-Skript zum Senden von E-Mails funktioniert nicht

mail_sender.php

<?php 
if(isset($_POST['submit'])){ 
    $to = "[email protected]"; 
    $from = $_POST['email']; 

    $message = " You received the fallowing message:" . "\n\n" . $_POST['message']; 


    mail($to,$message,$from); 
    echo "Mail Sent. Thank you, we will contact you shortly."; 
} 
?> 

HTML

<form action="mail_sender.php" method="post"> 
    <textarea id="email" name="email" rows="1" cols="30" placeholder="Type your email"></textarea> 
    <textarea id="formContent" name="message" rows="6" cols="30" placeholder="Type your message"></textarea> 
    <input type="submit" id="submit" value="Send"> 
</form> 
+0

Bitte beachten Sie, dass mit der 'mail verbinden 'Funktion in PHP ist eine Garantie für Ihre E-Mails im Spam-Ordner landen. Verwenden Sie ein Skript wie [PHPmailer] (https://github.com/PHPMailer/PHPMailer) mit einem SMTP-Server, um es korrekt zu übermitteln. – s1h4d0w

+0

Führen Sie dieses Skript lokal aus? –

+0

@HamzaZafeer Ja, ich führe dieses Skript auf localhost – mati

Antwort

0

Vor allem name-Attribut ist in Ihrem Submit-Button fehlt. Und php mail Funktion ist falsch.

es sein sollte:

$subject = "Your subject"; 
$headers = "From: $from "; 
mail($to,$subject,$message,$headers); 

statt:

mail($to,$message,$from); 
0

Standard mail() Funktion PHP funktioniert nicht die meisten der Zeit, vor allem mit GMail. Dies liegt daran, dass Ihre E-Mails auf eine spezielle Weise formatiert werden müssen, damit sie vom Google Mail-Server verwendet werden können. Sie werden besser eine Mail-Bibliothek wie PHPMailer verwenden.

So senden Sie eine E-Mail mit PHPMailer von einem GMail-Konto.

$mail = new PHPMailer(); 

    // ---------- adjust these lines --------------------------------------- 
    $mail->Username = "[email protected]"; // your GMail user name 
    $mail->Password = "passwd"; // your GMail Password 
    $mail->AddAddress("[email protected]"); // recipients email 
    $mail->FromName = "Your Name"; // readable name 

    $mail->Subject = "Subject"; 
    $mail->Body = "Body"; 

    $mail->Host = "smtp.gmail.com"; 
    $mail->Port = 465; // or 587 
    $mail->IsSMTP(); // use SMTP 
    $mail->SMTPAuth = true; // turn on SMTP authentication 
    $mail->From = $mail->Username; 

    //---------------------------------------------------------------------- 

    if(!$mail->Send()) 
    { 
     echo "mail sent"; 
    } 
0

Ich habe alles versucht und jetzt erhielt ich Nachricht SMTP-Fehler: Fehler beim Server und SMTP-Verbindung() fehlgeschlagen

 <form action="mail_sender.php" method="post"> 
     <textarea id="email" name="email" rows="1" cols="30" placeholder="Type your email"></textarea> 
     <textarea id="formContent" name="message" rows="6" cols="30" placeholder="Type your message"></textarea> 
     <input type="submit" name="submit" id="submit" value="Send"> 
    </form> 

PHP

<?php 
require "PHPMailer-master/PHPMailerAutoload.php"; 

$mail = new PHPMailer(); 

$to = "[email protected]"; // required 
$from = $_POST['email']; 

$comment = 
'Email: '.$from.' <br> /> 
Message: '.$_POST['message'].' <br> />' 
; 


$mail->Username = "[email protected]"; // your GMail user name 
$mail->Password = "Password"; // your GMail Password 
$mail->AddAddress("[email protected]"); // recipients email 
$mail->setFrom($_POST['email']); 
$mail->Body = 'This is the HTML message body <b>in bold!</b>'; 
$mail->SMTPSecure = 'ssl'; 
$mail->Host = "smtp.gmail.com"; 
$mail->Port = 465; // or 587 
$mail->SMTPDebug = 1; 
$mail->IsSMTP(); // use SMTP 
$mail->SMTPAuth = true; // turn on SMTP authentication 
$mail->Subject = 'Here is the subject'; 

//---------------------------------------------------------------------- 

if(!$mail->send()) { 
echo 'Message could not be sent.'; 
echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 
echo 'Message has been sent'; 
    } 
    ?> 
Verwandte Themen