Hi, I just test mail function using WordPress, when I click on the submit button the page redirecting on the specific page because I use an action tag. but the problem is mail not send, but when I hit the page normally it works. please check it I can give full access if you want.
thanks
URL- https://coustomwordpressmail.000webhostapp.com/
redirect URL - https://coustomwordpressmail.000webhostapp.com/confirmm/
Code
<?php /* Template Name: Home */ ?>
</style>
</head>
<body>
<div>
<form method="POST" action="<?php echo site_url('/confirmm') ?>">
<input type="text" name="name">
<input type="password" name="pass">
<input type="submit" name="sub">
</form>
</div>
</body>
</html>
Confirm page code
<?php /* Template Name:confirm */ ?>
<?php
$host='localhost';
$pass='forwordpress';
$db='id11731254_forwordpress';
$con = mysqli_connect($host,$db,$pass,$db);
$name = 'sssss';
$pass = 'fffff';
$qr="INSERT INTO user_detail (name,pass) VALUES ('$name','$pass')";
$ex = mysqli_query($con,$qr);
$to = "onqanetdev@gmail.com";
$subject = "Currency Detail & User Detail";
$message = $name;
$sendmail=mail( $to, $subject, $message);
echo '<pre>';
print_r($sendmail);
echo '</pre>';
if($sendmail==true)
{
echo 'Mail Send Success';
}
else{
echo 'no';
}
?>
The mail()
function is not very reliable, try using SMTP.
I just tried this
<?PHP
$sender = 'xxxxxxxx@gmail.com';
$recipient = 'xxxxxx@gmail.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
}
?>
On your account and it worked without issue, if you face issues with your script try using SMTP as suggested above.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require $_SERVER['DOCUMENT_ROOT'] . '/mail/Exception.php';
require $_SERVER['DOCUMENT_ROOT'] . '/mail/PHPMailer.php';
require $_SERVER['DOCUMENT_ROOT'] . '/mail/SMTP.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
$mail->Host = gethostbyname('smtp.gmail.com'); // use $mail->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Port = 587; // TLS only
$mail->SMTPSecure = 'tls'; // ssl is deprecated
$mail->SMTPAuth = true;
$mail->Username = '################@gmail.com'; // email
$mail->Password = '################'; // password
$mail->setFrom('################@gmail.com', 'NAME HERE PLEASE ########'); // From email and name
$mail->addAddress('################', 'NAME HERE PLEASE ################'); // to email and name
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body"); //$mail->msgHTML(file_get_contents('contents.html'), __DIR__); //Read an HTML message body from an external file, convert referenced images to embedded,
$mail->AltBody = 'HTML messaging not supported'; // If html emails is not supported by the receiver, show this body
// $mail->addAttachment('images/phpmailer_mini.png'); //Attach an image file
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "Message sent!";
}
?>