View Full Version : 'Send to Friend' - yet another fool struggling with the basics :-)

02-22-2007, 09:17 PM
I'm trying to implement a php "send to friend" form that I found online, but the emails are not getting sent...

There's one other post in this forum about the very same script, and that guy had the same problem, but it turned out that his problem was related to the eail address he had been using to test the form. (The code is by BigLickMedia.com, BTW - their 'recommend site' code)

I reckon the problem has nothing to do with the code, but some Host Provider setting I'm unaware of.

In the config part of the code, I set the variable $webmasterEmail to a new email address I just created for my domain especialy to handle the 'Send To Friend' stuff, which is diverted to a gmail account. Am I being naive in thinking that the new email address should work immediately, or does it take a few hours/days for it to be 'active'?

I haven't set any spam filter yet, so that's not the problem.

Not knowing much about php (or web hosting in general!), I was wondering if there's some obvious 'setting' (for want of a better word - forgive my ignorance) that the online code neglected to mention I should 'set' with my host provider (Infinology.com). Some cgi-bin thing, php.ini, or some 'permission'...? I'm floundering here! :-)

I ask this vaguely in the hope that there is something obvious. Well, anyway, despite being 99% sure that there's nothing wrong with the code, here is the critical part, just in case:

if(empty($errormessages)) {
$emailsubject = str_replace('[name]', $_POST['name'], $emailsubject);
$emailsubject = str_replace('[email]', $_POST['email'], $emailsubject);
$emailmessage = str_replace('[name]', $_POST['name'], $emailmessage);
$emailmessage = str_replace('[url]', $url, $emailmessage);
$emailmessage .= "\r\n\n" .
$_POST['message'] .
"\n\n\n\nBlah blah.";
$emailheader = "From: " . $_POST['email'] . "\r\n" .
"Reply-To: " . $_POST['email'] . "\r\n";

$sent = array();
foreach($_POST['to'] as $key=>$value) {
if(mail($value, $emailsubject, $emailmessage, $emailheader)) {
$sent[] = $value;
$failed = array_diff($_POST['to'], $sent);
$mailsent = true;

if($receiveNotifications) {
$subject = 'Someone recommended your site';
$message = 'This is a message to tell you that ' . $_POST['name'] . ' (' . $_POST['email'] .')' .
' sent a website recommendation to ' . implode(', ', $sent) .
"\n\nMessage: " . $_POST['message'];
$headers = 'From: ' . $webmasterEmail . "\r\n" .
'send-to-friend service for mathesonbayley.com';
@mail($webmasterEmail, $subject, $message, $headers);

Sorry if 'php' is not the right place for this quesiton - but I figured that the host 'setting' I might be looking for probably has something to do with php.

Thanks a lot for any guidance.

02-22-2007, 09:29 PM
If your hosting company has PHP installed correctly, the mail() function should work without any extra steps required by you.

You can see what the PHP settings are by simply inserting phpinfo(); into a script and viewing that script in a browser.

If you're wondering if your email address works yet, you can always send youself an email from your gmail account :p

And finally, my debugging suggestions are to remove the "@" that precedes the call to the mail() function (the "@" suppresses error messages and you NEED all the error messages you can get while debugging), and if you still aren't getting anywhere, print all of the strings you use in the mail() function, and check to see if mail() is returning true or false.

print "Sending email with:<br />\n";
print "address: $webmasterEmail<br />\n";
print "subject: $subject<br />\n";
print "message: $message<br />\n";
print "header: $headers<br />\n";

if (!mail($webmasterEmail, $subject, $message, $headers)) {
print "failed to send email.<br />\n";
} else {
print "email has been sent.<br />\n";

02-22-2007, 10:07 PM
Just something else to consider - for the first loop that sends the emails, normally, none of the To: addresses will be hosted on your mail server and the From: address won't be hosted on your mail server (for your testing they probably will be, but not for random people visiting your site.) Most mail servers will consider any email where none of the To/From addresses are hosted on that mail server to be relaying and won't process the email.

It is possible that your mail server has been setup to "trust" the IP address of your web server and scripts are allowed to sent emails that would normally be considered relaying. If this is not the case on your server, you will need to use "authentication" against a valid email account on the mail server. Unfortunately, the mail(...) function cannot authenticate and you would need to use something like the phpmailer class.

02-22-2007, 10:26 PM
Very informative, clear and curteous response (to answer your 'tag'!) - Thanks a lot.

I sent an email from my gmail and, yep, the new email address is working!
(doh!) :-)

Taking out the @ was a great tip, thanks.
I got this error:

Warning: mail() expects at least 3 parameters, 0 given

Echoing all the variables, all was as expected, i.e. correct.
I can't figure out how to echo the array mail(), however... echo $mail(); doesn't work. What does?

So, if zero parameters were given for mail(), then does that mean that the page with the "Your email was successfully sent" message is not receiving the variables? Does that require 'GET'?

The page refreshes upon submitting the form, by the way, with $_SERVER['PHP_SELF'] as the 'action', in case that's relevant or useful.