douchui4815 2015-04-02 20:53
浏览 214

PHPMailer无法发送邮件

I am trying to use PHPMailer to send data input into a contact form to another email address. I followed the Build A PHP Contact Form but I cant seem to get it to work. This is my 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>
    <head>
        <meta charset="utf-8">
        <title>Contact Form</title>

        <link rel="stylesheet" href="css/main.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="name" autocomplete="off" <?php echo isset($fields['name']) ? ' value="' . e($fields['name']) . '"' : '' ?>>
                </label>
                <label>
                    Your email address *
                    <input type="text" 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']);

?>

And this is my 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.';
            }
        }

        if(empty($errors)) {

            $mail = new PHPMailer;


            $mail->SMTPAuth = true;

            $mail->SMTPDebug = false;

            $mail->Host = 'smtp.gmail.com';
            $mail->Username = 'email@gmail.com';
            $mail->Password =  'password';
            $mail->SMTPSecure = 'ssl';
            $mail->Port = 465;

            $mail->isHTML(true);

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

            $mail->FromName = 'Contact';

            $mail->AddAddress('email@gmail.com', 'name');

            if($mail->send()) {
                header('Location: thanks.php');
            } else {
                $errors[] = 'Sorry, could not send email. Try again later.';
            }

        }

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

$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
header('Location: index.php')

?>

When I try to submit the contact form I get the error "Sorry, could not send email. Try again later." Could anyone help me figure out what is going wrong? Thanks in advance

When I used print_r($mail), it output the following:

2015-04-03 12:37:40 CLIENT -> SERVER: EHLO 127.0.0.1 2015-04-03 12:37:40    CLIENT -> SERVER: STARTTLS 
Warning: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in C:\xampp\htdocs\contact_form\libs\phpmailer\class.smtp.php on line 339
2015-04-03 12:37:40 CLIENT -> SERVER: QUIT 2015-04-03 12:37:40  SMTP ERROR: QUIT command failed: 2015-04-03 12:37:40    SMTP connect() failed. 
PHPMailer Object ( [Version] => 5.2.9 [Priority] => 3 [CharSet] => iso-8859-1 [ContentType] => text/html [Encoding] => 8bit [ErrorInfo] => SMTP connect() failed. [From] => root@localhost [FromName] => Contact [Sender] => [ReturnPath] => [Subject] => Contact form submitted [Body] => From: Your name field (your email address field)

your message field

[AltBody] => 
[Ical] => 
[MIMEBody:protected] => From: Your name field (your email address field)
your message field

[MIMEHeader:protected] => Date: Fri, 3 Apr 2015 14:37:40 +0200 To: Name From: Contact Subject: Contact form submitted Message-ID: <9c632471cedadb7f5e7ee6687ee9d5b7@127.0.0.1> X-Priority: 3 X-Mailer: PHPMailer 5.2.9 (https://github.com/PHPMailer/PHPMailer/) MIME-Version: 1.0 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: 8bit 
[mailHeader:protected] => 
[WordWrap] => 0 
[Mailer] => smtp 
[Sendmail] => /usr/sbin/sendmail [UseSendmailOptions] => 1 
[PluginDir] => 
[ConfirmReadingTo] => 
[Hostname] => 
[MessageID] => 
[MessageDate] => Fri, 3 Apr 2015 14:37:40 +0200 
[Host] => smtp.gmail.com 
[Port] => 587 
[Helo] => 
[SMTPSecure] => tls 
[SMTPAuth] => 1 
[Username] => email@gmail.com 
[Password] => password 
[AuthType] => 
[Realm] => 
[Workstation] => 
[Timeout] => 300 
[SMTPDebug] => 1 
[Debugoutput] => echo 
[SMTPKeepAlive] => 
[SingleTo] => 
[SingleToArray] => Array ( ) 
[do_verp] => 
[AllowEmpty] => 
[LE] => 
[DKIM_selector] => 
[DKIM_identity] => 
[DKIM_passphrase] => 
[DKIM_domain] => 
[DKIM_private] => 
[action_function] => 
[XMailer] => 
[smtp:protected] => SMTP Object ( [Version] => 5.2.9 [SMTP_PORT] => 25 [CRLF] => [do_debug] => 1 [Debugoutput] => echo [do_verp] => [Timeout] => 300 [Timelimit] => 300 [smtp_conn:protected] => [error:protected] => Array ( ) [helo_rply:protected] => [server_caps:protected] => [last_reply:protected] => ) 
[to:protected] => Array ( [0] => Array ( [0] => email@gmail.com [1] => Name ) ) 
[cc:protected] => Array ( ) 
[bcc:protected] => Array ( ) 
[ReplyTo:protected] => Array ( ) 
[all_recipients:protected] => Array ( [email@gmail.com] => 1 ) 
[attachment:protected] => Array ( ) 
[CustomHeader:protected] => Array ( ) 
[lastMessageID:protected] => <9c632471cedadb7f5e7ee6687ee9d5b7@127.0.0.1> 
[message_type:protected] => plain 
[boundary:protected] => Array ( [1] => b1_9c632471cedadb7f5e7ee6687ee9d5b7 [2] => b2_9c632471cedadb7f5e7ee6687ee9d5b7 [3] => b3_9c632471cedadb7f5e7ee6687ee9d5b7 ) 
[language:protected] => Array ( [authenticate] => SMTP Error: Could not authenticate. [connect_host] => SMTP Error: Could not connect to SMTP host. [data_not_accepted] => SMTP Error: data not accepted. [empty_message] => Message body empty [encoding] => Unknown encoding: [execute] => Could not execute: [file_access] => Could not access file: [file_open] => File Error: Could not open file: [from_failed] => The following From address failed: [instantiate] => Could not instantiate mail function. [invalid_address] => Invalid address [mailer_not_supported] => mailer is not supported. [provide_address] => You must provide at least one recipient email address. [recipients_failed] => SMTP Error: The following recipients failed: [signing] => Signing Error: [smtp_connect_failed] => SMTP connect() failed. [smtp_error] => SMTP server error: [variable_set] => Cannot set or reset variable: ) 
[error_count:protected] => 1 
[sign_cert_file:protected] => 
[sign_key_file:protected] => 
[sign_key_pass:protected] => 
[exceptions:protected] => ) 
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\contact_form\contact.php:53) in C:\xampp\htdocs\contact_form\contact.php on line 63
  • 写回答

1条回答 默认 最新

  • dongwang6837 2015-04-03 13:24
    关注

    I figured out how to do it. I made the following changes to my 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.';
                }
            }
    
            if(empty($errors)) {
    
                $mail = new PHPMailer();
    
                $mail->IsSMTP();
    
                //$mail->SMTPAuth = true;
    
                $mail->SMTPDebug = 1;
    
                $mail->Host = 'aspmx.l.google.com' ;
                //$mail->Username = 'email@gmail.com';
                //$mail->Password =  'password';
                //$mail->SMTPSecure = 'tls';
                $mail->Port = 25;
    
                $mail->isHTML(true);
    
                $mail->Subject = 'Contact form submitted';
                $mail->Body = 'From: ' . $fields['name'] . ' (' . $fields['email'] . ')<p>' . $fields['message'] . '</p>';
    
                $mail->FromName = 'Contact';
    
                $mail->AddAddress('email@gmail.com', 'Name');
    
    
    
                if($mail->send()) {
                    header('Location: thanks.php');
                } else {
                    $errors[] = 'Sorry, could not send email. Try again later.';
                    //print_r($mail);
                }
    
            }
    
    } else {
            $errors[] = 'Something went wrong.';
    }
    $_SESSION['errors'] = $errors;
    $_SESSION['fields'] = $fields;
    header('Location: index.php')
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog