(php初学者帮助)如果您打算使用smtp,请在此行后添加smtp代码

hpcdzsge  于 2021-09-29  发布在  Java
关注(0)|答案(2)|浏览(380)

我是php&html新手,需要帮助使用php发送电子邮件。我使用的代码是一个web模板,其中包含有文档化的代码,这可能是一个简单的修复。
问题:当我在live server中为联系人表单发送测试电子邮件并按“发送”时,什么都没有发生,没有电子邮件通过,也没有成功消息。我的php代码中是否缺少某些内容,或者我是否需要设置smtp?

  • 我的知识是基础的,所以这个问题可能被问错了。谢谢你的反馈!
<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';

// If you intend you use SMTP, uncomment next line
require 'phpmailer/src/SMTP.php';

// Set the recipient email address here
$recipients = array();

$recipients[] = array(
    'email' => 'contact@westonborst.com',
    'name' => 'Weston'
);

// Set the sender email address here
$sender = array(
    'email' => 'contact@westonborst.com',
    'name' => 'Weston'
);

// reCaptcha Secret Key - Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = '';

// PHPMailer Initialization
$mail = new PHPMailer();

// If you intend you use SMTP, add your SMTP Code after this Line

// End of SMTP

// Form Messages
$message = array(
    'success'           => 'Thank you for your message. It has been sent.',
    'error'             => 'There was an error trying to send your message. Please try again later.',
    'error_bot'         => 'Bot Detected! Message could not be send. Please try again.',
    'error_unexpected'  => 'There was an unexpected error trying to send your message. Please try again later.',
    'recaptcha_invalid' => 'Captcha not Validated! Please Try Again.',
    'recaptcha_error'   => 'Captcha not Submitted! Please Try Again.'
);

// Form Processor
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

    $prefix    = !empty( $_POST['prefix'] ) ? $_POST['prefix'] : '';
    $submits   = $_POST;
    $botpassed = false;

    $message_form                 = !empty( $submits['message'] ) ? $submits['message'] : array();
    $message['success']           = !empty( $message_form['success'] ) ? $message_form['success'] : $message['success'];
    $message['error']             = !empty( $message_form['error'] ) ? $message_form['error'] : $message['error'];
    $message['error_bot']         = !empty( $message_form['error_bot'] ) ? $message_form['error_bot'] : $message['error_bot'];
    $message['error_unexpected']  = !empty( $message_form['error_unexpected'] ) ? $message_form['error_unexpected'] : $message['error_unexpected'];
    $message['recaptcha_invalid'] = !empty( $message_form['recaptcha_invalid'] ) ? $message_form['recaptcha_invalid'] : $message['recaptcha_invalid'];
    $message['recaptcha_error']   = !empty( $message_form['recaptcha_error'] ) ? $message_form['recaptcha_error'] : $message['recaptcha_error'];

    // Bot Protection
    if( isset( $submits[ $prefix . 'botcheck' ] ) ) {
        $botpassed = true;
    }

    if( !empty( $submits[ $prefix . 'botcheck' ] ) ) {
        $botpassed = false;
    }

    if( $botpassed == false ) {
        echo '{ "alert": "error", "message": "' . $message['error_bot'] . '" }';
        exit;
    }

    // reCaptcha
    if( isset( $submits['g-recaptcha-response'] ) ) {

        $recaptcha_data = array(
            'secret' => $recaptcha_secret,
            'response' => $submits['g-recaptcha-response']
        );

        $rc_verify = curl_init();
        curl_setopt( $rc_verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify" );
        curl_setopt( $rc_verify, CURLOPT_POST, true );
        curl_setopt( $rc_verify, CURLOPT_POSTFIELDS, http_build_query( $recaptcha_data ) );
        curl_setopt( $rc_verify, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $rc_verify, CURLOPT_RETURNTRANSFER, true );
        $rc_response = curl_exec( $rc_verify );

        $g_response = json_decode( $rc_response );

        if ( $g_response->success !== true ) {
            echo '{ "alert": "error", "message": "' . $message['recaptcha_invalid'] . '" }';
            exit;
        }
    }

    $html_title = !empty( $submits['html_title'] ) ? $submits['html_title'] : 'Form Response';
    $forcerecaptcha = ( !empty( $submits['force_recaptcha'] ) && $submits['force_recaptcha'] != 'false' ) ? true : false;
    $replyto = !empty( $submits['replyto'] ) ? explode( ',', $submits['replyto'] ) : false;

    if( $forcerecaptcha ) {
        if( !isset( $submits['g-recaptcha-response'] ) ) {
            echo '{ "alert": "error", "message": "' . $message['recaptcha_error'] . '" }';
            exit;
        }
    }

    $mail->Subject = !empty( $submits['subject'] ) ? $submits['subject'] : 'Form response from your website';
    $mail->SetFrom( $sender['email'] , $sender['name'] );

    if( !empty( $replyto ) ) {
        if( count( $replyto ) > 1 ) {
            $replyto_e = $submits[ $replyto[0] ];
            $replyto_n = $submits[ $replyto[1] ];
            $mail->AddReplyTo( $replyto_e , $replyto_n );
        } elseif( count( $replyto ) == 1 ) {
            $replyto_e = $submits[ $replyto[0] ];
            $mail->AddReplyTo( $replyto_e );
        }
    }

    foreach( $recipients as $recipient ) {
        $mail->AddAddress( $recipient['email'] , $recipient['name'] );
    }

    $unsets = array( 'prefix', 'subject', 'replyto', 'message', $prefix . 'botcheck', 'g-recaptcha-response', 'force_recaptcha', $prefix . 'submit' );

    foreach( $unsets as $unset ) {
        unset( $submits[ $unset ] );
    }

    $fields = array();

    foreach( $submits as $name => $value ) {
        if( empty( $value ) ) continue;

        $name = str_replace( $prefix , '', $name );
        $name = ucwords( str_replace( '-', ' ', $name ) );

        if( is_array( $value ) ) {
            $value = implode( ', ', $value );
        }

        $fields[$name] = $value;
    }

    $response = array();

    foreach( $fields as $fieldname => $fieldvalue ) {
        $response[] = $fieldname . ': ' . $fieldvalue;
    }

    $referrer = $_SERVER['HTTP_REFERER'] ? '<br><br><br>This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';

    $body = implode( "<br>", $response ) . $referrer;

    $mail->MsgHTML( $body );
    $sendEmail = $mail->Send();

    if( $sendEmail == true ):
        if( $autores && !empty( $replyto_e ) ) {
            $send_arEmail = $autoresponder->Send();
        }

        echo '{ "alert": "success", "message": "' . $message['success'] . '" }';
    else:
        echo '{ "alert": "error", "message": "' . $message['error'] . '<br><br><strong>Reason:</strong><br>' . $mail->ErrorInfo . '" }';
    endif;

} else {
    echo '{ "alert": "error", "message": "' . $message['error_unexpected'] . '" }';
}

?>
gstyhher

gstyhher1#

不知道为什么它不会发送,这是我试过的吗?有什么建议吗(我从代码中删除了我的密码)

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';

// If you intend you use SMTP, uncomment next line
require 'phpmailer/src/SMTP.php';

// Set the recipient email address here
$recipients = array();

$recipients[] = array(
    'email' => 'borstweston@gmail.com',
    'name' => 'Weston'
);

// Set the sender email address here
$sender = array(
    'email' => 'borstweston@gmail.com',
    'name' => 'Weston'
);

// reCaptcha Secret Key - Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = '';

// PHPMailer Initialization
$mail = new PHPMailer();

// If you intend you use SMTP, add your SMTP Code after this Line
$mail->IsSMTP(); // enable SMTP
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com.";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "borstweston@gmail.com";
$mail->Password = "";
$mail->SetFrom("borstweston@gmail.com");
$mail->Subject = "";
$mail->Body ="";
$mail->AddAddress($sendTo);

// End of SMTP

// Form Messages
$message = array(
    'success'           => 'Thank you for your message. It has been sent.',
    'error'             => 'There was an error trying to send your message. Please try again later.',
    'error_bot'         => 'Bot Detected! Message could not be send. Please try again.',
    'error_unexpected'  => 'There was an unexpected error trying to send your message. Please try again later.',
    'recaptcha_invalid' => 'Captcha not Validated! Please Try Again.',
    'recaptcha_error'   => 'Captcha not Submitted! Please Try Again.'
);

// Form Processor
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

    $prefix    = !empty( $_POST['prefix'] ) ? $_POST['prefix'] : '';
    $submits   = $_POST;
    $botpassed = false;

    $message_form                 = !empty( $submits['message'] ) ? $submits['message'] : array();
    $message['success']           = !empty( $message_form['success'] ) ? $message_form['success'] : $message['success'];
    $message['error']             = !empty( $message_form['error'] ) ? $message_form['error'] : $message['error'];
    $message['error_bot']         = !empty( $message_form['error_bot'] ) ? $message_form['error_bot'] : $message['error_bot'];
    $message['error_unexpected']  = !empty( $message_form['error_unexpected'] ) ? $message_form['error_unexpected'] : $message['error_unexpected'];
    $message['recaptcha_invalid'] = !empty( $message_form['recaptcha_invalid'] ) ? $message_form['recaptcha_invalid'] : $message['recaptcha_invalid'];
    $message['recaptcha_error']   = !empty( $message_form['recaptcha_error'] ) ? $message_form['recaptcha_error'] : $message['recaptcha_error'];

    // Bot Protection
    if( isset( $submits[ $prefix . 'botcheck' ] ) ) {
        $botpassed = true;
    }

    if( !empty( $submits[ $prefix . 'botcheck' ] ) ) {
        $botpassed = false;
    }

    if( $botpassed == false ) {
        echo '{ "alert": "error", "message": "' . $message['error_bot'] . '" }';
        exit;
    }

    // reCaptcha
    if( isset( $submits['g-recaptcha-response'] ) ) {

        $recaptcha_data = array(
            'secret' => $recaptcha_secret,
            'response' => $submits['g-recaptcha-response']
        );

        $rc_verify = curl_init();
        curl_setopt( $rc_verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify" );
        curl_setopt( $rc_verify, CURLOPT_POST, true );
        curl_setopt( $rc_verify, CURLOPT_POSTFIELDS, http_build_query( $recaptcha_data ) );
        curl_setopt( $rc_verify, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $rc_verify, CURLOPT_RETURNTRANSFER, true );
        $rc_response = curl_exec( $rc_verify );

        $g_response = json_decode( $rc_response );

        if ( $g_response->success !== true ) {
            echo '{ "alert": "error", "message": "' . $message['recaptcha_invalid'] . '" }';
            exit;
        }
    }

    $html_title = !empty( $submits['html_title'] ) ? $submits['html_title'] : 'Form Response';
    $forcerecaptcha = ( !empty( $submits['force_recaptcha'] ) && $submits['force_recaptcha'] != 'false' ) ? true : false;
    $replyto = !empty( $submits['replyto'] ) ? explode( ',', $submits['replyto'] ) : false;

    if( $forcerecaptcha ) {
        if( !isset( $submits['g-recaptcha-response'] ) ) {
            echo '{ "alert": "error", "message": "' . $message['recaptcha_error'] . '" }';
            exit;
        }
    }

    $mail->Subject = !empty( $submits['subject'] ) ? $submits['subject'] : 'Form response from your website';
    $mail->SetFrom( $sender['email'] , $sender['name'] );

    if( !empty( $replyto ) ) {
        if( count( $replyto ) > 1 ) {
            $replyto_e = $submits[ $replyto[0] ];
            $replyto_n = $submits[ $replyto[1] ];
            $mail->AddReplyTo( $replyto_e , $replyto_n );
        } elseif( count( $replyto ) == 1 ) {
            $replyto_e = $submits[ $replyto[0] ];
            $mail->AddReplyTo( $replyto_e );
        }
    }

    foreach( $recipients as $recipient ) {
        $mail->AddAddress( $recipient['email'] , $recipient['name'] );
    }

    $unsets = array( 'prefix', 'subject', 'replyto', 'message', $prefix . 'botcheck', 'g-recaptcha-response', 'force_recaptcha', $prefix . 'submit' );

    foreach( $unsets as $unset ) {
        unset( $submits[ $unset ] );
    }

    $fields = array();

    foreach( $submits as $name => $value ) {
        if( empty( $value ) ) continue;

        $name = str_replace( $prefix , '', $name );
        $name = ucwords( str_replace( '-', ' ', $name ) );

        if( is_array( $value ) ) {
            $value = implode( ', ', $value );
        }

        $fields[$name] = $value;
    }

    $response = array();

    foreach( $fields as $fieldname => $fieldvalue ) {
        $response[] = $fieldname . ': ' . $fieldvalue;
    }

    $referrer = $_SERVER['HTTP_REFERER'] ? '<br><br><br>This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';

    $body = implode( "<br>", $response ) . $referrer;

    $mail->MsgHTML( $body );
    $sendEmail = $mail->Send();

    if( $sendEmail == true ):
        if( $autores && !empty( $replyto_e ) ) {
            $send_arEmail = $autoresponder->Send();
        }

        echo '{ "alert": "success", "message": "' . $message['success'] . '" }';
    else:
        echo '{ "alert": "error", "message": "' . $message['error'] . '<br><br><strong>Reason:</strong><br>' . $mail->ErrorInfo . '" }';
    endif;

} else {
    echo '{ "alert": "error", "message": "' . $message['error_unexpected'] . '" }';
}

?>
bxgwgixi

bxgwgixi2#

如果你把所有的东西都填好了,这就行了(我试过了)

require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
require 'PHPMailer/Exception.php';

$mail = new PHPMailer\PHPMailer\PHPMailer();

try {
    $mail->IsSMTP(); // enable SMTP
    $mail->CharSet = 'UTF-8';
    $mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true; // authentication enabled
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465; // or 587
    $mail->IsHTML(true);
    $mail->Username = "Your Email Address";
    $mail->Password = "Your Password";
    $mail->SetFrom("From Name");
    $mail->Subject = "Message Subject";
    $mail->Body ="Message Body ";
    $mail->AddAddress('Sent email to this address');

    if($mail->Send()) {
        // e-posta başarılı ile gönderildi
      echo 'success';
    } else {
        echo 'error';
    }
} catch (Exception $e) {
    echo 'error : '.$e;
}

如果没有,请分享您收到的错误消息。

相关问题