TerryR Posted July 30, 2006 Share Posted July 30, 2006 >:(I just want to send emails using the mail() function. It returns True, but doesn't actually send the email! Any ideas please? Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted July 30, 2006 Share Posted July 30, 2006 Do you know if your code's correct? or what? Post your code and we could take a look. Quote Link to comment Share on other sites More sharing options...
tomfmason Posted July 30, 2006 Share Posted July 30, 2006 here is a simple script. It should work fine.[code=php:0]<?phpif (isset($send) { if ((!$to) || (!$subject) || (!$message)) { echo "You did not submit the following required information"; if (!$to) { echo "Must enter a email address to send the mail"; } if (!$subject) { echo "You must enter a subject"; } if (!message) { echo "You must enter a message"; } exit; }$to = $_POST['to'];$subject = $_POST['subject'];$message = $_POST['message'];$to = stripslashes($to);$subject = stripslashes($subject);$message = stripslashes($message);mail($to, $subject, $message, "From: You<you@yoursite.com>\nX-Mailer: PHP/" . phpversion()) or die("Unable to send mail"); echo "Your email was sent successfuly";}?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Send mail Script</title></head><body><form action="send.php" method="post"><p>To</p><p><input type="text" name="to" size="20"></p><p>Subject</p><p><input type="text" name="subject" size="20"></p><p>Message</p><p><textarea cols="38" rows="10" name="message"></textarea></p><input type="submit" name="send" value="Send"></form></body></html>[/code] Quote Link to comment Share on other sites More sharing options...
TerryR Posted July 30, 2006 Author Share Posted July 30, 2006 Thanks for the quick replies. Here's my simplified test code that still won't work:<?php// The message$message = "Line 1\nLine 2\nLine 3";// Send$result=mail('terry.richter@virgin.net', 'My Subject', $message);if ($result){ echo "true";} else echo "False";?> The function returns True (= 1), but either the mail is being sent into a black hole in cyberspace, or isn't being sent at all. I'm certainly not receiving anything! Quote Link to comment Share on other sites More sharing options...
tomfmason Posted July 30, 2006 Share Posted July 30, 2006 try that code I posted above You will need to change the From You<you@yoursite.com> To your actual email address. Also you will need to name it send.php Quote Link to comment Share on other sites More sharing options...
silver32 Posted July 30, 2006 Share Posted July 30, 2006 you forgot to set senders e-mail, should be $result=mail('terry.richter@virgin.net', 'My Subject', $message, "From: You<you@you.com>" ); Quote Link to comment Share on other sites More sharing options...
para11ax Posted July 30, 2006 Share Posted July 30, 2006 The host I use recently put some restrictions on mail() to avoid it being hijacked for spamming easily. They require a good amount of headers to be submitted with the mail() for it to go through:[code="php"]$headers = "From: MyName <".$fromemail.">\r\n";$headers .= "Reply-To: <".$fromemail.">\r\n";$headers .= "X-Sender: <".$fromemail.">\r\n";$headers .= "X-Mailer: PHP4\r\n"; //mailer$headers .= "X-Priority: 3\r\n"; //1 UrgentMessage, 3 Normal$headers .= "Return-Path: <".$fromemail.">\r\n";$headers .= "MIME-Version: 1.0\r\n";$headers .= "Content-Type: text/html; charset=iso-8859-1\n\n\r\n";mail($toemail, $subject, $message, $headers);[/code](you define $fromemail, $toemail, $subject, $message before this)This might be worth a shot if you are still having problems. Otherwise you can try contacting your host to see if they have disabled it (or check the phpinfo() output to see). Hope this helps. Quote Link to comment Share on other sites More sharing options...
onlyican Posted July 30, 2006 Share Posted July 30, 2006 Some mail systems auto delete emails without proper headers, your format does not have any headersthe From does not always matter (it will be from nobody@server.com or simular)my Basic PHP Mail[code=php:0]$to = "info@example.com";$subject = "This is an Email";$body = "This is the email body,<br /> I tend to use HTM format\n";$headers = 'MIME-Version: 1.0' . "\r\n";$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; //This is for HTM Format$headers .= 'To: Name<'.$_to.'>,' . "\r\n";$headers .= 'From: CompanyName<info@example.net>' . "\r\n";if(mail($to, $subject, $body, $headers){echo "Mail should have sent";}else{echo "There must have been an error}[/code]If this fails, you need to look at setting the informationI think it was php_ini["smtp"];I cant remember tho, so dont hold me against that Quote Link to comment Share on other sites More sharing options...
TerryR Posted July 30, 2006 Author Share Posted July 30, 2006 Thanks to all replies, but sorry, I've tried them all and still no luck. What should I look for in phpinfo to see if mailing has been disabled? Quote Link to comment Share on other sites More sharing options...
otuatail Posted July 30, 2006 Share Posted July 30, 2006 Instead of"From: You<you@yoursite.com>\nX-Mailer: PHP/""From: You<you@yoursite.com>\r\n"and something like this before the mail functionini_set("sendmail_from", "root@des-otoole.co.uk"); Quote Link to comment Share on other sites More sharing options...
TerryR Posted July 30, 2006 Author Share Posted July 30, 2006 Still no progress. It just says the mail has been sent. Maybe it has - but I sure ain't receiving it! Quote Link to comment Share on other sites More sharing options...
redarrow Posted July 30, 2006 Share Posted July 30, 2006 the mail might go in a spam folder check ok. Quote Link to comment Share on other sites More sharing options...
TerryR Posted July 30, 2006 Author Share Posted July 30, 2006 Thanks for the suggestion, but I don't have a spam folder set up on the account I'm using. I'm using Outlook Express, and can send emails to myself perfectly well. Quote Link to comment Share on other sites More sharing options...
onlyican Posted August 1, 2006 Share Posted August 1, 2006 Its not the account which makes the mail go to SPAM accountIts to do with Mail Headers, the mail system, Your POP3 Account Holder, Before outlook express reads mails.This can tell if the email come from a website or pop 3 account ...do you have spam assasin running on your account (Check Cpanel)try the same thing with a Hotmail account or something Quote Link to comment Share on other sites More sharing options...
TerryR Posted August 1, 2006 Author Share Posted August 1, 2006 I set up a new Hotmail account and tried it, but still no joy. This is the actual code I'm using, though I have tried the other suggestions too. The file is called mailtest3.php. I get the "Your email was sent successfuly" message, but no message arrives.<?phpif (isset($_POST['btnSend'])) { $to = $_POST['to']; $subject = $_POST['subject']; $message = $_POST['message']; if ((!$to) || (!$subject) || (!$message)) { echo "You did not submit the following required information:\n"; if (!$to) { echo "Must enter a email address to send the mail\n"; } if (!$subject) { echo "You must enter a subject\n"; } if (!message) { echo "You must enter a message\n"; } exit; } else { $to = stripslashes($to); $subject = stripslashes($subject); $message = stripslashes($message); mail($to, $subject, $message, "From: me<terry654321@hotmail.co.uk>\nX-Mailer: PHP/") or die("Unable to send mail"); echo "Your email was sent successfuly"; }}else {echo "btnSend not set";}?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Send mail Script</title></head><body><form action="mailtest3.php" method="post"><p>To</p><p><input type="text" name="to" size="40" value = 'terry654321@hotmail.co.uk'></p><p>Subject</p><p><input type="text" name="subject" size="40" value = 'xxx' ></p><p>Message</p><p><textarea cols="38" rows="10" name="message">time is now <?php time() ?> </textarea></p><input type="submit" name="btnSend" value="Send"></form></body></html> Quote Link to comment Share on other sites More sharing options...
Chetan Posted August 1, 2006 Share Posted August 1, 2006 [code]mail($to, $subject, $message, "From: me<terry654321@hotmail.co.uk>\nX-Mailer: PHP/") or die("Unable to send mail"); [/code]thats not error handling[code]if(mail($to, $subject, $message, "From: me<terry654321@hotmail.co.uk>\nX-Mailer: PHP/") or die("Unable to send mail")){echo "postd";}else{echo "no post";} [/code]this should tell you the truth and put ini_set("display_errors", "1"); in the starting, befor anything. Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted August 1, 2006 Share Posted August 1, 2006 go to this line:[code]mail($to, $subject, $message, "From: me<terry654321@hotmail.co.uk>\nX-Mailer: PHP/") or die("Unable to send mail"); echo "Your email was sent successfuly";[/code]See, it will automatically give you that message, try to put an IF stement.So, try this:[code]if(mail($to, $subject, $message, "From: me<terry654321@hotmail.co.uk>\nX-Mailer: PHP/")){ echo "Your email was sent successfuly";}[/code]I'm new to PHP so IDK if that syntax is right, hopefully someoen can correct me if im wrong. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted August 1, 2006 Share Posted August 1, 2006 You may need to use the little known and little used fifth parameter to the mail() function that sets the "Return-path:" header. Many sites, including Hotmail, Yahoo, and AOL are rejecting or just dropping the messages if the domain in the Return-Path header doesn't match that in the "From:" header. It won't if you're using shared hosting. To use:[code]<?php//// all your other header set up//$fifth = '-f email@your.domain.here';mail($to,$subject,$body,$headers,$fifth);?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
TerryR Posted August 1, 2006 Author Share Posted August 1, 2006 I take the point about the If statement, and the fifth parameter, so I've now got $fifth = '-f terry654321@hotmail.co.uk'; if(mail($to, $subject, $message, "From: me<terry654321@hotmail.co.uk>\nX-Mailer: PHP/",$fifth)) { echo "posted"; } else { echo "no post"; } ... and it still doesn't work! Quote Link to comment Share on other sites More sharing options...
tomfmason Posted August 1, 2006 Share Posted August 1, 2006 Do you have your own mail server or are you useing a hosting service? The reason that I ask is some mail servers require that you identify your self as a known sender. Here is an example of a simple way to send mail with Mecury Mail.[code=php:0]/*This should be an email address known to the server. Unless you are using Hot mail to as your out going mail server, do not use your hotmail address.Use an email address that would be known to the mail server*/$knownsender = "you@yoursite.com"; $mailtos = "email";// the email address the mail is being sent to $subject = "Your subject";$message = "Your message";}if ($ccaddress=="" || $ccaddress==" "){ $header="From: $knownsender";}else{ $header .="From: $knownsender\r\n"; $header .=" Cc: $ccaddress";}// $header.="Bcc: $bcaddress";if (@mail($mailtos, $subject, $message, $header)){ echo "It worked";}else{ echo "There was an error sending your email";}[/code] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted August 1, 2006 Share Posted August 1, 2006 There are some questions that haven't been asked of the OP yet.1) Is this one a shared hosting machine or one host per machine?2) Is this your own machine or are you buying (or using) site space, if so who is hosting your site?3) Which web server software is being used?4) Windows or Unix/Linux?5) If this is a Unix/Linux box do you have shell access?6) Is there a web based interface to the administration end of the host, for example cPanel?7) What version of PHP?8.) Have you talked to your Tech Support about looking at the mail logs?The answers are all relevent to trying to get an answer to your problem.Ken Quote Link to comment Share on other sites More sharing options...
tomfmason Posted August 1, 2006 Share Posted August 1, 2006 I am glad the grace/smite is gone but I would have graced you for that kenrbnsn. Quote Link to comment Share on other sites More sharing options...
TerryR Posted August 1, 2006 Author Share Posted August 1, 2006 Ken,Here are my answers:1. I've tried on two hosts, both with many users - does that mean shared?2. I'm buying site space on one server, but at present trying things out on my college system3. I don't know4. My machine is Windows XP5. n/a6. Yes, for the site I am buying, but no for my college site7. 5.1.48. Not yet, but I will tomorrow Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 1, 2006 Share Posted August 1, 2006 This may be a stupid question, but what are your setting in php.ini?SMTP = mail.mailserver.comsmtp_port = 25sendmail_from = yourname@mailserver.nlROnald ;D Quote Link to comment Share on other sites More sharing options...
TerryR Posted August 1, 2006 Author Share Posted August 1, 2006 Ronald, I think you might just have cracked it for me. This is what I've got:SMTP = localhostsmtp_port = 25; For Win32 only.;sendmail_from = me@example.comIt feels wrong to have localhost, but I don't know what to have instead. Could you explain a bit more please? Quote Link to comment 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.