0

I've set a mail function with PHP Mailer and the mail is being received but it doesn't redirect after, this is the code:

//more code

require("includes/class.phpmailer.php");
$mail = new PHPMailer();

$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress("mytargetaddres@example.com");

$mail->WordWrap = 50; 
$mail->IsHTML(true);     
$mail->Subject  =  "Contact";
$mail->Body     =  "my body";

$mail->IsSMTP(); 
$mail->Host = "my.host";
$mail->SMTPAuth = true; 
$mail->Username = "mytargetaddres@example.com";
$mail->Password = "mypassword";

if($mail->Send()){
    header('Location: thanks.php');
}else{
    header('Location: error.php');
}

I've the exact same setup in another website on the same server and it works but here instead of redirecting it shows a blank page.

Edit: after showing erros it says this:

SMTP Error: Data not accepted.

Warning: Cannot modify header information - headers already sent by (output started at /public_html/mail.php:1) in /public_html/mail.php on line 37

Line 37 is: header('Location: error.php');

nick
  • 2,819
  • 5
  • 33
  • 69
  • Better check for errors first with: `error_reporting(E_ALL); ini_set('display_errors', 1); /* SET IT TO 0 ON A LIVE SERVER! */` – PajuranCodes Mar 13 '18 at 17:54
  • 3
    Take a look at your http servers error log file. Most likely your script outputs something _prior_ to you using the `header()` function. – arkascha Mar 13 '18 at 17:54
  • Edited my question – nick Mar 13 '18 at 17:58
  • Depending on your version of PHPMailer, you might have to use `$mail->setFrom ( $email, $name );` instead. – IcedAnt Mar 13 '18 at 18:00
  • @arkascha is right: you are printing (maybe with _echo_) something before sending the response header with _header()_. I suppose you are doing it in the code part before the _require_ statement. – PajuranCodes Mar 13 '18 at 18:02
  • @IcedAnt tried but it throws 500 error – nick Mar 13 '18 at 18:05
  • @arkascha the code before the require is just $_REQUEST to take the data from the form, there's no echoing :( – nick Mar 13 '18 at 18:05
  • It is a well known facepalm effect in php development to have trailing white spaces after a closing php tag for example that leads to _unwanted_ output getting sent. That is why I asked you to look into your http servers error log file to read the actual error message thrown. Without that you are fishing in the dark... – arkascha Mar 13 '18 at 18:08
  • @arkascha wow that's really stupid, but I think that was the problem: saved the file with utf8 instead of utf8 with BOM and now it works (-‸ლ) – nick Mar 13 '18 at 18:14
  • 1
    Whyever one wants to use different character encodings today... glad things work for you. Have fun! – arkascha Mar 13 '18 at 18:15

0 Answers0