'If you intend you use SMTP, add your SMTP Code after this Line
When I do a test email for a contact form in Live Server and press 'Send' nothing happens, no email goes through and no success message. Am I missing something in my PHP code or do I need to setup 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' => '[email protected]',
'name' => 'Weston'
);
// Set the sender email address here
$sender = array(
'email' => '[email protected]',
'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'] . '" }';
}
?>
Not sure why it won't send, here is what I tried? Any advise? (And I removed my password from the code)
<?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' => '[email protected]',
'name' => 'Weston'
);
// Set the sender email address here
$sender = array(
'email' => '[email protected]',
'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 = "[email protected]";
$mail->Password = "";
$mail->SetFrom("[email protected]");
$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'] . '" }';
}
?>
Solution 1:[1]
If you fill everything correctly this should work. (I have tried it)
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;
}
if not please share the error message you are getting.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |