JonnyDriller Posted January 30, 2020 Share Posted January 30, 2020 (edited) 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 January 30, 2020 by JonnyDriller Quote Link to comment https://forums.phpfreaks.com/topic/309951-echo-a-message-on-header-location-return/ Share on other sites More sharing options...
Barand Posted January 30, 2020 Share Posted January 30, 2020 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> Quote Link to comment https://forums.phpfreaks.com/topic/309951-echo-a-message-on-header-location-return/#findComment-1573912 Share on other sites More sharing options...
JonnyDriller Posted January 30, 2020 Author Share Posted January 30, 2020 @Barand You're a legend ! Thx so much, most of the day I'm trying to do that... Finally I can sleep... Quote Link to comment https://forums.phpfreaks.com/topic/309951-echo-a-message-on-header-location-return/#findComment-1573914 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.