PHP邮件程序无法通过SMTP

时间:2016-10-19 11:24:52

标签: php phpmailer

我制作了一个PHP邮件程序,但它不再工作了。它曾经工作,但我没有改变任何东西。它没有发送电子邮件。我填写了所有必要的信息,但它没有。我知道我的问题不太清楚,因为我不知道问题是什么,如果涉及PHP,我不是很好。 这是代码。

这是INDEX.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="en">
<head>
<meta charset="UTF-8">
<title>Contact form</title>

<link rel="stylesheet" href="css/style.css"/>
<script src="js/test.js"></script>
</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="name" autocomplete="off" <?php echo     isset($fields['name']) ? 'Value="' . e($fields['name']) . '"' : '' ?>>
        </label>
        <label>
            Your email address *
            <input type="email" name="email" 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>
    </form>
</div>



</body>
</html>

<?php
unset($_SESSION['errors']);
unset($_SESSION['fields']);
?>

这是CONTACT.PHP

<?php

session_start();

require_once "libs/phpmailer/PHPMailerAutoload.php";

$errors = [];


if(isset($_POST['name'], $_POST['email'], $_POST['message'])) {

$fields = [
    'name' => $_POST['name'],
    'email' => $_POST['email'],
    "message" => $_POST['message']
];

foreach($fields as $field => $data) {
    if(empty($data)){
        $errors[] = 'The ' . $field . ' field is required.';
    }
}

    // 587 is voor uitgaande email deze is SSL en SMTP.ziggo.nl
    // 993 is voor inkomende email deze is TLS en IMAP.ziggo.nl
    // 110 is voor inkomende email deze is POP3 en
if(empty($errors)){

    $mail = new PHPMailer;

    $mail->isSMTP();
    $mail->SMTPAuth = true;

    $mail->Host = 'smtp1.example.com';
    $mail->Username = 'example';
    $mail->Password = 'secret';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 69;

    $mail->isHTML();

    $mail->Subject = 'Contact form submitted';
    $mail->Body = 'From: ' . $fields['name'] . ' ('. $fields['email'] .') <p>'. $fields['message'] .'</p>';

    $mail->FromName = 'Contact';

    $mail->AddAddress('email', 'name');

    if($mail->send()){
        header('Location: bedankt.php');
        die();
    } else {
        $errors[] = 'Sorry we konden de email niet verzenden, Probeer later  nog een keer.';
    }
}

} else {
$errors[] = 'Something went wrong.';
}

$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;

header('location: index.php');

我希望你们能帮助我提供我在这里提供的信息。 如果缺少某些东西请说出来

更新 问题已经解决了。我与代码无关,但它与主持人是一个isseu,谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

First: In contact.php are you sure about port 69 and why not 587 ? if you are using ziggo the $mail->Host must be smtp.ziggo.nl Uitgaande serverinstellingen server: smtp.ziggo.nl serverpoort: 587 Use ssl: type versleuteling: STARTTLS, TLS of SSL Be carefull with $mail->isSMTP(); that it is true $mail->isSMTP(true);

working example:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
require $path.'/phpmailer/PHPMailerAutoload.php'; //the downloaded zip with PHPMailerAutoload i have put into a directoru called phpmailer!!
//require 'PHPmailer.php';

$_error = '';

 // insert configs
 $_smtpAuth = 'true';  //true
 $_smtpSecure = 'SSL';   // TLS og SSL
 $_smtpHost = 'send.one.com';  //one.com has send.one.com //gmail has smtp.gmail.com
 $_smtpPort = '465';
 $_smtpUsername = 'xxx@qwerty.com'; //your email address or username
 $_smtpPassword = 'xxxXxxxy';   //your password

 $mail = new PHPmailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP(true);                                      // Set mailer to use SMTP
$mail->Host     = $_smtpHost;//$_smtpHost.';smtp2.example.com';       // Specify main and backup SMTP servers
$mail->SMTPAuth = $_smtpAuth;                         // Enable SMTP authentication
$mail->Username = $_smtpUsername;                     // SMTP username
$mail->Password = $_smtpPassword;                     // SMTP password
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = $_smtpPort;                             // TCP port to connect to

$mail->setFrom('name@yourwebmail.com', 'Mailer');
$mail->addAddress('', '');     // Add a recipient
$mail->addAddress('');               // Name is optional
$mail->addReplyTo('', ''); //$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('name@awebmail.com');  //Recipient  The sender of the email
$mail->addBCC('');

$mail->addAttachment('');         // Add attachments
$mail->addAttachment('');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

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

?>

答案 1 :(得分:-1)

                    date_default_timezone_set('Europe/Berlin');
                    require("class.phpmailer.php");

                    $mail = new phpmailer();
                    $mail->IsSMTP();                                   // per SMTP verschicken
                    $mail->Host     = "example.com"; // SMTP-Server
                    $mail->SMTPAuth = true;     // SMTP mit Authentifizierung benutzen
                    $mail->Username = "example";  // SMTP-Benutzername
                    $mail->Password = "example"; // SMTP-Passwort

                    $mail->From     = "test@example.com";//any address
                    $mail->FromName = "example";
                    $mail->AddAddress($to,"");
                    $mail->IsHTML(true);                               // als HTML-E-Mail senden
                    $mail->Subject  =  "Confirmation mail ";
                    $mail->Body     =  "<html><body>Dear example <br/><br/><br/>  \nKind regards\n<br/><br/> KM </body></html>";

                    if(!$mail->Send())
                    {
                        echo "error";
                    }
                    else
                    {
                        echo "send";
                    }