-
PHP mail-function
I'm having a little problem here...
I'm starting up my own e-business, and I'm writing it all myself (in PHP).
To sign up, users must provide their name, email, pick a username and a password.
After they click the "submit"-link, an email is sent to the mailaddress they provided, and that's where the problem is situated...
Here's my code:
Code:
$mailSubject = "Confirm Your membership.";
$mailBody = "Hi $firstname $lastname,\n";
$mailBody .= "\n";
$mailBody .= "Thank you for joining ********\n";
$mailBody .= "\n";
$mailBody .= "Please click the following link to confirm your membership. If clicking does\n";
$mailBody .= "not work, please copy and paste into your browser manually.\n";
$mailBody .= "\n";
$mailBody .= "http://*****.***.**\n";
$mailBody .= "\n";
$mailBody .= "Thank you.\n";
$mailHeaders = "From: [email protected] \n";
$mailHeaders = "ReplyTo: [email protected] \n";
mail($email, $mailSubject, $mailBody, $mailHeaders);
The mail is sent, but the problem seems to be in the headers... the mail seems to be coming from [email protected].... That's pretty cool, but there is no [email protected] (that address doesn't exist). And it should be coming from [email protected].
I'm guessing that there is something wrong with my headers (or the formatting). Priorweb is my host, so I'm guessing (again) that they decline my headers and assign the anonymous-thingy to it... any suggestions?
-
hm, that's weird... the syntax you [posted should be working? have you tried removing that extra line brake at the end of the headers?
-
maybe u can get some hints from these to e-mail forms:
- contactme from www.stevedawson.com - free, its very configurable, and easy to do it.
or allstuffmailer from http://www.Frenck.nl - also free, didnt try it
or do a search on www.scriptsearch.com
good luck
-
This is weird... I took out the $Mailheader variables and manually added them in the script, and it works now...
Code:
$mailSubject = "******** - Confirm Your membership.";
$mailBody = "Hi $firstname $lastname,\n";
$mailBody .= "\n";
$mailBody .= "Thank you for joining ********!\n";
$mailBody .= "\n";
$mailBody .= "Please click the following link to confirm your membership. If clicking does\n";
$mailBody .= "not work, please copy and paste into your browser manually.\n";
$mailBody .= "\n";
$mailBody .= "http://********.***.**\n";
$mailBody .= "\n";
$mailBody .= "Thank you.\n";
mail($email, $mailSubject, $mailBody, "From: [email protected]");
That's what you get for using variables :rolleyes:
-
Perhaps $mailheader is being used by the hosting company as a global php variable which would override a local one, if I'm not mistaken....perhaps try it with a rephrased variable name
-
The problem was that you were assigning $mailHeaders a new value with the Reply-To line, rather than concatenating the string. I.e.:
should have been:
If you want to be even more fancy, you can do this:
which will show up as your name in most email clients/web based interfaces. It's a bit more friendly than an email address (you can replace Negative with say "Postmaster" or whatever you want).
I got fed up of messing with the headers manually whenever I wanted to send mail, so I wrote a class to do the work for me. Now all I have to do is call the appropriate method (i.e. sender_email($email, $name)) and it adds the headers automatically. The source code is available if you want to see how it's done.
BTW, the ReplyTo field is a bit redundant IMO if you're going to set it to the same value as the From field.
Oh, and if a value isn't specified for the From: field (as it wasn't in your case, because you were overwriting the string with the ReplyTo: field), PHP will use whatever's defined in php.ini. I used to get emails from '[email protected]' before I figured out how to fix this (I'd accidentally put the To: headers in the wrong place, or used From: twice or something like that).
Hope this helps!
-
I'm pretty noob on php but interested in...
would you like to try this?
-
Quote:
Originally posted here by stanger
I'm pretty noob on php but interested in...
would you like to try this?
stanger:
a) I've already pointed that out
b) You've made the same (albeit easy to make) mistake as Negative, you're overwritting the value of $mailHeaders with the second assignment as opposed to concatenating the string
-
hmmm...
a) i was to slow ;)
b) however...
that php file is the wrong place to set the email reply address
(if things go to database and $postmastermail should be global)
mail($email, $mailSubject, $mailBody, $mailHeaders, $postmastermail)
would this work?
or should be:
mail($email, $mailSubject, $mailBody, $mailHeaders "<$postmastermail>")
???
-
Mr. Waring, you are a genius... or I'm an airhead, whichever works...
Thank you! Problem solved :D
-
stanger: Neither of those would work, unless you changed the $postmastermail variable to be an command line argument to, for example, sendmail.
If the same email is always going to be used as the From: address, it'd be better to define it as a constant in a globally-included PHP file, e.g.:
define('POSTMASTER_EMAIL', '[email protected]');
define('POSTMASTER_NAME', 'Negative');
then you'd have:
$mailHeaders = 'From: ' . POSTMASTER_NAME . ' <' . POSTMASTER_EMAIL . '>' . "\n";
I use a similar method on my sites (except I pass the constants as an argument to a method of my mail class, rather than editing the headers manually in each script) and it makes life a *lot* easier.
Quote:
Originally posted here by Negative
Mr. Waring, you are a genius... or I'm an airhead, whichever works...
Neither - I'm no genius with PHP and it was an easy mistake to make - I've made a similar one in the past and it took me ages to track it down. There's no better teacher than experience! ;)
-
Neg, I had problems with the mail function aswell.
For some reason '\n' wasn't working to end header, to, subject, message fields and I was getting weird error messages, so after a little research I found that as a hold-over from ancient times '\r\n' is more acceptable.
But, I am by no means submitting that I actually understand or am correct in this, I just know that I was trying everything, but as I am not a scientist I think my last attempt combined two changes and it worked thereafter, so I don't know if it was the first or the second change.
My problem was a different, I had a form to contact one out of a set of administrative users, but didn't want to put mailto: links, so I've got a select drop-down menu that keeps UIDs and then queries the DB to get the addy and then send.
Peace,
Dhej
-
I use "\r\n" in my scripts (and you have to use double quotes or else it won't be interpolated) and it seems to work. I don't know if there is a difference between the two (I know there is when dealing with ASCII files - \n is the line ending on Unix, \r on Mac and \r\n on Windows) for mail but I've always used \r\n and it's always worked.
Dhej: That drop-down menu idea is a good one, I especially like the concept of keeping the email addresses in a database because that way they can't be harvested by spam bots (because the addresses won't even be in the HTML source code output to the browser). I've never had anyone other than me working on my sites, so I've never needed more than one email (which is why I used define() rather than a database query) but it's a good idea nonetheless.
-
ahh cool, so I'm not completely wrong with the "\r\n", and I looked at my script and in my headers I've used \n between things like X-Mailer: and such, but ended the section with /r/n
I can't take credit for the drop down, that was acually suggested to me by my "client" (my friend that I am making the site for)., but it sure made things easier for a lot of things, I've got the script so that it won't let the same IP send to the same email addy more than twice in 50 min, it logs all vitals to database, and should mail() return false it save the message body in the DB aswell, which has actually save my butt a couple of times. (Like when I accidentally botched the script with a minor modification to the headers... I've got it set up so that it will catch the UID through $_GET aswell, so that I can have anyplace that a username pops up have a link to contact.
There is only 4 usernames on the site, which filter into 2 people, which makes it condusive to the select menu, too many more and it would be too long I think.
Cheers,
Dhej