PHPmailer联系表单

时间:2015-12-29 12:14:41

标签: phpmailer

我需要一点帮助。我使用PHPmailer作为联系表单。 当我测试PHPmailer时,它工作得很完美,我收到了从脚本发送的电子邮件,但当我尝试将其连接到联系表单时...我没有收到任何电子邮件(甚至没有垃圾文件夹)或错误。

我做错了什么?

<?php
ini_set('display_errors',  true);
error_reporting(1);

if(isset($_POST['submit1'])) 
{

$message=
'Full Name: '.$_POST['name'].'<br />
Subject:    '.$_POST['subject'].'<br />
from:   '.$_POST['email'].'<br />
Comments:   '.$_POST['message'].'
';
    require 'PHPMailerAutoload.php';


    $mail = new PHPMailer;  

    // Set up SMTP  
    $mail->IsSMTP();               
    $mail->SMTPAuth = true;        
    $mail->SMTPSecure = "ssl";      
    $mail->Host = "mail.mys4e.com";  
    $mail->Port = 587;  
    $mail->Encoding = '7bit';

    // Authentication  
    $mail->Username   = "neal@mys4e.com"; 
    $mail->Password   = "********"; 

    // Compose
    $mail->SetFrom($_POST['email'], $_POST['name']);
    $mail->AddReplyTo($_POST['email'], $_POST['name']);
    $mail->Subject = "New Contact Form Enquiry";      
    $mail->MsgHTML($message);

    // Send To  
    $mail->AddAddress("neal@mys4e.com", "Neal Fourie"); 
    $result = $mail->Send();          
    $message = $result ? 'Successfully Sent!' : 'Sending Failed!';      
    unset($mail);

}
?>

HTML

                    <form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
                        <div class="form-group">
                            <input type="text" name="name" class="form-control" placeholder="Name" required>
                        </div>
                        <div class="form-group">
                            <input type="email" name="email" class="form-control" placeholder="Email" required>
                        </div>
                        <div class="form-group">
                            <input type="text" name="subject" class="form-control" placeholder="Subject" required>
                        </div>
                        <div class="form-group">
                            <textarea name="message" class="form-control" rows="8" placeholder="Message" required></textarea>
                        </div>
                        <button type="submit" name="submit1" class="btn btn-primary">Send Message</button>
                    </form>

1 个答案:

答案 0 :(得分:0)

试试这个它对我来说很好

ini_set('display_errors',  true);
require_once "PHPMailerAutoload.php";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$Comments = $_POST['message'];


$mail = new PHPMailer;

//Enable SMTP debugging. 
$mail->SMTPDebug = 3;                               
//Set PHPMailer to use SMTP.
$mail->isSMTP();            
//Set SMTP host name                          
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;                          
//Provide username and password     
$mail->Username = "ahmedkhangaditek@gmail.com";                 
$mail->Password = "yourpassword";                           
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";                           
//Set TCP port to connect to 
$mail->Port = 587;                                   

$mail->From = "ahmedkhangaditek@gmail.com";
$mail->FromName = "Ahmed Khan";

$mail->addAddress($email, $name);

$mail->isHTML(true);

$mail->Subject = $subject;
$mail->Body = $Comments;
$mail->AltBody = $Comments;

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}