Jump to content

Echo a message on header location return


JonnyDriller
 Share

Recommended Posts

I have this working script below. I never managed to turn it into a function... :( I think it's because there's classes in it. 

 

I stopped that investigation and instead I choose to:- keep it on another page, trigger it with the a <form> button, then return to the form page using header location.

This is probably much more elaborate than it needs to be... but it seems to be working fine. 

 

I'd like to print out "email sent" if successful. The collection of confusion at the end of this script, is me trying to get it to return the - "email sent" message

<index.php>

<?php

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

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

$mail = new PHPMailer();

$mail->isSMTP();

$mail->Host = "smtp.gmail.com";

$mail->SMTPAuth = "true";

$mail->SMTPSecure = "tls";

$mail->Port = "587";

$mail->Username = "email@gmail.com";

$mail->Password = "pass";

$mail->Subject = "Test email using PhPmailer";

$mail->setFrom("email@gmail.com");

$mail->isHTML(true);

//$mail->addAttachment('img/movie.mp4');

$mail->Body = "<h1>This is a HTML heading</h1></br><p>This is a html title</p>";

$mail->addAddress("email@gmail.com");

if ($mail->Send() ) {
	echo "Email sent..!";	
}
else{
	echo "Error..!";
}

$mail->smtpClose();

ob_start();
$Message = urlencode("Email Sent");
header("Location:EmailBut.html?Message=".$Message);
die;
ob_flush();
?>

The stuff at the beginning of this next script is me trying to get it to print out email sent. 

I can get it to return the message in the URL but it won't print out. 

<EmailBut.html>


<?php
ob_start();
if(isset($_GET['Message'])){
    echo $_GET['Message'];
}

echo $_GET['Message'];
ob_flush();
?>

<html>
<body>
<form action="index.php" method="POST">
<button type="submit">Send</button>
</form>
</body>
</html>

Can you please give me a push in the right direct here? 

Edited by JonnyDriller
Link to comment
Share on other sites

Lose the output buffering (ob_ functions).

Rename "EmailBut.html" to "EmailBut.php" so that php code is executed.

The first code should end like this

    .
    .
    .
if ($mail->Send() ) {
    $Message = "Email sent..!";    
}
else{
    $Message = "Error..!";
}

$mail->smtpClose();

$Message = urlencode($Message);
header("Location:EmailBut.php?Message=$Message");
?>

EmailBut.php

<?php
if(isset($_GET['Message'])){
    echo $_GET['Message'];
}
?>
<html>
<body>
<form action="index.php" method="POST">
<button type="submit">Send</button>
</form>
</body>
</html>

 

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.