0

Hi I used a tutorial for learning the moderately difficult header information required to send an email with an optional attachment in php. I modified it a little bit and it was working as I expected, until I changed it to match exactly the tutorial for testing purposes. As soon as I tested it I checked my email a few seconds later and 21 emails had already been sent to me. I deleted the file from the server right away and closed the page. I am wondering what exactly caused this behavior.

Immediately after the mail() function I set a redirect to the page I wanted.

if (mail($mailto, $subject, "", $header)) {
    header("Location: $redirectUrl");
    exit;       
} 

where $header is half a page long. Is it the header() function causing it? The page just says page is taking too long to redirect. Do I have a problem with escaped / non-escaped characters? I call the function only one time and it is not included within any kind of for loop or do while. The page itself is not included anywhere else.

6
  • 1
    Well, what's $redirectUrl? There's nothing wrong with the code as it is now, unless $redirectUrl causes header() to redirect you back to the same script. Oct 6, 2012 at 20:04
  • It just redirects to a separate page on the same site but different directory.
    – Matt
    Oct 6, 2012 at 20:09
  • PHP's mail() function is awful. I would strongly recommend using a decent mail class like phpMailer. It will make your life easier, no matter what you're using mail() for.
    – Spudley
    Oct 6, 2012 at 20:10
  • @Matt Is it possible that $redirectUrl is actually empty? If that happens, you can have unpredictable behavior with the redirect. Try setting it to a hardcoded value and see what happens. Oct 6, 2012 at 20:12
  • @NullUserException Hmm... no, but maybe it's out of scope? I have it defined at the top of my script, and then I call my mail_with_attachment function(), where the mail() function itelsef is inside, and I didn't pass the variable to the function. Is that why?
    – Matt
    Oct 6, 2012 at 20:21

1 Answer 1

0

NullUserException brought up a good point. The variable was out of scope. A very simple change fixed the script. Originally the variable was declared outside the function, and not visible within the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.