问题
I'm currently using XAMPP and PHPMailer for a simple 'CONTACT ME' email form on my test-website. The problem is as follows. I cannot seem to be able to send out a test email to make sure if it is working. I've spent the majority of my weekend trying to find out the root of this problem but I still cannot seem to find an answer. It does not even tell me if something went wrong or not.
I've tried the following according to the web/ StackOverFlow/ Documentation:
- removed semicolon from php.ini for line "extension=php_openssl.dll"
- restarted XAMMP
- updated port and host values *seen below*
- DLd PHPMailer from https://github.com/PHPMailer/PHPMailer
- watched PHPAcademy's "Build A PHP Contact Form" Video on Youtube
- Actually 95% of his code is his...just doesn't work for me
- Left a comment on his video. No response as of yet.
I've looked through most of the questions on this site and came up with nothing that can resolve my problem. I'm reading through quiet a few of documentation and so far. I seem to be doing the right thing. Therefore it baffles me that this simple form is not working for me.
contact.php
<?php
session_start();
require_once 'helpers/security.php';
$errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : [];
$fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : [];
?>
<!DOCTYPE html>
<html lang="eng">
<head>
<meta charset="UTF-8">
<title>Contact Form</title>
<link rel="stylesheet" href="indexContactStyle.css">
</head>
<body>
<div class="contact">
<?php if( !empty($errors)) : ?>
<div class="panel">
<ul> <li> <?php echo implode('</li><li>', $errors ); ?> </li> </ul>
</div>
<?php endif ?>
<form action="contact.php" method="post">
<label>
Your Name * <input type="text" name="inputName" autocomplete="off" <?php echo isset($fields['name']) ? ' value= " '. e($fields['name']) .' " ' : '' ?> >
</label>
<label>
Your Email * <input type="text" name="inputEmail" autocomplete="off" <?php echo isset($fields['email']) ? ' value= " '. e($fields['email']) .' " ' : '' ?> >
</label>
<label>
Your Message * <textarea name="message" rows="8"><?php echo isset($fields['message']) ? e($fields['message']) : '' ?></textarea>
</label>
<input type="submit" value="Send">
<p class="muted"> * means a required field</p>
<button class="muted" type="button" onClick="goBack()">Back</button>
</form>
</div>
</body>
</html>
<?php
unset($_SESSION['errors']);
unset($_SESSION['fields']);
?>
index.php
<?php
session_start();
require_once 'libs/phpMailer/PHPMailerAutoload.php';
$errors = [];
print_r($_POST);
if (isset($_POST['inputName'], $_POST['inputEmail'], $_POST['message'])) {
$fields = [
'name' => htmlentities( $_POST['inputName'] ),
'email' => $_POST['inputEmail'],
'message'=> $_POST['message']
];
foreach ($fields as $field => $data) {
if( empty($data) ) {
$errors[] = 'The ' . $field . 'field is required.';
}
}
if(empty($errors)) {
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPauth = true;
$m->SMTDebug = true;
$m->Host ='smtp.gmail.com';
$m->Username = 'myEmail@gmail.com';
$m->Password = 'myPass';
$m->SMTPSecure = 'ssl';
$m->Port = 465;
$m->isHTML(true);
$m->Subject = 'Contact form submitted';
$m->Body ='From: ' .$fields['name'] . ' ( ' . $fields['email'] . ' ) <p> ' . $fields['message'] . ' </p> ' ;
$m->FromName = 'Contact';
// Not entirely sure what is supposed to go here. Some ppl put their own email others the email of the sender. which one is it?
$m->AddAddress('myEmail@gmail.com', 'myName');
if ( $m->send() ) {
header('Location: thanks.php');
die();
} else {
$errors[] = 'Sorry could not send email. Try again later ';
}
}
} else {
$errors[] = 'Something went HORRIBLY WRONG!!';
}
$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
header('Location: contact.php');
回答1:
A few things you can try:
- PHP properties and variables are case-sensitive, so
SMTPauth
should beSMTPAuth
. SMTPDebug
takes an integer, not a boolean, so set it to 1 or above for debug output, 0 to turn it off.- For gmail, set
SMTPSecure
to 'tls', andPort
to 587. AddAdress
is used to add normal 'To' recipients - you call this for each person you want the message to go to (there can be more than one).- Don't call
die()
after successful sending - your script will terminate normally without that. - If you are generating any debug output, the redirect to your 'thanks' page will not work (you'll get a 'headers already sent' error), so whil you are debugging you might want to just echo something here instead so you know it's worked.
Much of the code looks like whoever wrote the course was working with an old version of PHPMailer.
回答2:
The following code works fine for me. Try this:
try
{
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = 'smtp';
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
$m->Username = 'myEmail@gmail.com';
$m->Password = 'myPass';
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->SingleTo = true;
$mail->From = "test@abc.com";
$mail->FromName = "Mailer";
$mail->AddAddress("ankit@yopmail.com", "Ankit Chugh"); //mail to to
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email vie wer!";
$mail->MsgHTML("Your html message");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
catch (phpmailerException $e) {
echo $e->errorMessage(); //error messages from PHPMailer
}
catch (Exception $e) {
echo $e->getMessage();
}
来源:https://stackoverflow.com/questions/26966995/phpmailer-will-not-work-with-xampp-no-errors-no-emails-not-working