Error in mail

I’m using PhpMailer to send emails and the script is running successfully but I’m receiving emails from my own email address but it should be from the one who entered their email address on my website.
Please help.

I want to send emails to my email id when someone send some message from my website’s contact form.

Try to copy this code (remember to have the latest version of PHPMailer, the master branch, for this):
<?php

use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\Exception;

require "./PHPMailer-master/src/Exception.php";
require "./PHPMailer-master/src/PHPMailer.php";
require "./PHPMailer-master/src/SMTP.php";

$mail = new PHPMailer(true);

// $mail->setLanguage("it", "./PHPMailer-master/language/");

$username = $_POST["username"];

$email = $_POST["email"];

$message = $_POST["message"];

try {

$mail->isSMTP();

$mail->Host = "ssl://smtp.gmail.com:465";

$mail->SMTPAuth = true;

$mail->Username = "[email protected]"; // overwrite this with the principal email ID, but make sure you have enabled Less Secure Apps and [done this here](https://accounts.google.com/DisplayUnlockCaptcha)!

$mail->Password = "********"; // overwrite those asterisks with your actual password, but make sure you don't post this in public places!

$mail->setFrom("[email protected]", "ErgastolatorOfficial 1"); // overwrite this with your principal email and name of contact

$mail->addAddress("[email protected]"); // overwrite this with the other contact email

$mail->addReplyTo($email);

$mail->isHTML(true);

$mail->Subject = "Ergastolator Website - Nuovo messaggio"; // translate this into English and change Ergastolator Website with the name of your website

$mail->Body = "Username: " . $username . " &lt;" . $email . "&gt; <br /> Messaggio: " . $message . " <br /><br /><br /><div style='text-align:right'>Inviato dal form di Ergastolator Website</div>"; // translate this into English and change "Ergastolator Website" with the name of your website

$mail->AltBody = "Non &egrave; possibile inviare la mail con questo client perch&eacute; completamente progettato senza HTML. Per favore installa un client supportato."; // translate this into English

$mail->send();

echo "Il tuo messaggio &egrave; stato inviato con successo!" // translate this into English

die();

} catch (Exception $e) {

echo "Il tuo messaggio non &egrave; stato inviato con successo a causa di un errore di configurazione. Errore Mailer: {$mail->ErrorInfo}"; // translate this into English

die();

}

?>

$username, $email and $message variables have a POST supervariable that get the POST contents from the input and textarea with name username, email and message, so please make sure you’re following the same pattern on your code.

1 Like

Thank you so much for your help. It worked! I was missing something and now everything is fine.
And one more thing, does port matter much? I’m using 587 with tsl.

1 Like

TLS port 587 for me it’s not working, so I tried to use port 465 and SSL mode and it worked for me. I hope it works for you.

yeah for me its working.

Can you suggest me something to secure our websites?

You can use SSL certificates to secure the website. The best way without complicated steps to do it is Cloudflare, that can be enabled via the cPanel integration or dash.cloudflare.com.

Alright. Thank you once again.:blush:

1 Like

I want to add an alert box on the page, once the mail has been sent successfully.
I tried using javascript alert inside echo, but it is not working

And also I want to clear the form after submission.

Then change this:

echo "Il tuo messaggio &egrave; stato inviato con successo!" // translate this into English

with this:

echo "Il tuo messaggio &egrave; stato inviato con successo! <br /><a href='yourcontactspage' >Ritorna alla pagina contatti</a>" // translate this into English

following the comment and overwriting yourcontactspage to where your contacts page is on that contact form.

1 Like

Why I’m not getting echos on screen, the messages are displaying only in the network activity in Dev Tools.

Then replace this:

echo "Il tuo messaggio &egrave; stato inviato con successo! <br /><a href='yourcontactspage' >Ritorna alla pagina contatti</a>" // translate this into English

into this:

header("Location: yourcontactsconfirmationpage");
die();

replacing yourcontactsconfirmationpage with the name of the new contact confirmation file that ends with the extension .html that you can create and put these contents in:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>Contact Form is Submitted</title>
</head>
<body>
<p>The contact form was sent successfully.</p>
<p><a href="yourcontactspage">Return to the contacts page</a></p>
</body>
</html>

, overwriting yourcontactspage with the contacts page you’re going to use for this example.

1 Like

Alright. And can you help with clearing the form data after submission?

After you clicked on the link “Return to the contacts page”, the form will be cleaned, so you will have an empty form. If you have saved the password on that website, and saved it on the Chrome Passwords Wallet, remove it and don’t save it again. Like so you can start with writing a new message with an empty form.

I’m not getting this still, I’m receiving the response only under the network tab. The page is showing as a response. how is it possible?

Can you please give the link of the contact form, so I can give a look and see what’s going on?

http://sixthinsight.cf/#contact

Here’s my website’s contact form. I’m not sure what’s the problem is now. Like I faced this first time that no response in printing on-screen.
Please check.

Try using this code in a new file, that you can include (make sure you’re using the master branch of PHPMailer, on github.com/PHPMailer/PHPMailer, before doing this):

<?php
    date_default_timezone_set("Etc/UTC");
	use PHPMailer\PHPMailer\PHPMailer;
	use PHPMailer\PHPMailer\Exception;

	require "./PHPMailer-master/src/Exception.php";
    require "./PHPMailer-master/src/PHPMailer.php";
    require "./PHPMailer-master/src/SMTP.php";

	$mail = new PHPMailer(true);
	// $mail->setLanguage("it", "./PHPMailer-master/language/");

	$fullname = $_POST["name"];
	$email = $_POST["email"];
	$message = $_POST["message"];
	try {
		$mail->isSMTP();
		$mail->Host = "ssl://smtp.gmail.com:465";
		$mail->SMTPAuth = true;
		$mail->Username = "[email protected]"; // change this with your Gmail email
		$mail->Password = "*******"; // change those asterisks with your Gmail password

		$mail->setFrom("[email protected]", "ErgastolatorOfficial 1"); // change this with your email and name of the sender
		$mail->addAddress("[email protected]"); // change this with the email address of the receiver
		$mail->addReplyTo($email);
		$mail->isHTML(true);
		$mail->Subject = "Ergastolator Website - Nuovo messaggio"; // translate this in English and change Ergastolator Website with your website name
		$mail->Body = "Nome completo: " . $fullname . " &lt;" . $email . "&gt; <br /> Messaggio: " . $message . " <br /><br /><br /><div style='text-align:right'>Inviato dal form di Ergastolator Website</div>"; // change Ergastolator Website with your website name and translate it all into English
		$mail->AltBody = "Non &egrave; possibile inviare la mail con questo client perch&eacute; completamente progettato senza HTML. Per favore installa un client supportato."; // translate it into English
		$mail->send();
		header("Location: ../messageSent"); // change this with the sending message OK confirmation link to the file
		die();
	} catch (Exception $e) {
		header("Location: ../messageFailed"); // change this with the sending message failed confirmation link to the file
        die();
	}
?>

and on the <button> element put type="submit" as type, and on the form element put method="POST" as method.

1 Like