473,806 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why doesnt my email send?

69 New Member
This is my script.....
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     $to = "example@example.co.uk";
  4.     $subject = "subject";
  5.  
  6.     //minimum characters allowed in the message box
  7.     $msg_min_chars = "10";
  8.  
  9.     //maximum characters allowed in the message box
  10.     $msg_max_chars = "250";
  11.  
  12.     function validate_form_items()
  13.     {
  14.        global $msg_min_chars, $msg_max_chars;
  15.        $msg_chars = "{".$msg_min_chars.",".$msg_max_chars."}";
  16.  
  17.        $form_items = array(
  18.  
  19.            "name"  => array(
  20.                            "regex" => "/^([a-zA-Z '-]+)$/",
  21.                            "error" => "Name appears to be in inproper format",
  22.                            ),
  23.             "email" => array(
  24.                            "regex" =>
  25.                             "/^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/",
  26.                            "error" => "Please enter a valid email address",
  27.                            ),
  28.            "message" => array(
  29.                            "regex" => "/^.{$msg_chars}$/",
  30.                            "error" => "Your message is either too short or exceeds $msg_max_chars characters",
  31.                            ),
  32.        );
  33.  
  34.        $errors = array();
  35.  
  36.        foreach($form_items as $item_name => $item_props)
  37.        {
  38.            if (!preg_match($item_props["regex"], trim($_POST[$item_name])))
  39.            {
  40.                    $errors[] = $item_props["error"];
  41.            }
  42.        }
  43.  
  44.        return $errors;
  45.     }
  46.  
  47.     function email($from, $to, $subject, $message)
  48.     {
  49.         $headers = "From: ".$from."\r\n";
  50.         $headers .= "Reply-To: ".$from."\r\n";
  51.         $headers .= "Return-Path: ".$from."\r\n";
  52.  
  53.         if (mail($to,$subject,$message,$headers) ) {
  54.             echo "Thank you for your inquiry, Your message has been received. <br>We will get back to you as soon as we can.";
  55.         } else {
  56.             echo "Your message could not be sent at this time. Please try again.";
  57.         }
  58.     }
  59.  
  60.     function print_error($errors)
  61.     {
  62.         foreach($errors as $error)
  63.         {
  64.             echo $error."<br>";
  65.         }
  66.     }
  67.  
  68.     function form_process()
  69.     {    
  70.         global $to, $subject;
  71.  
  72.         $errors = validate_form_items();
  73.  
  74.         if(count($errors) == 0)
  75.         {
  76.             $errors [] = email(trim($_POST["email"]), $to, $subject, $_POST["message"]);
  77.         }
  78.  
  79.         print_error($errors);
  80.     }
  81.  
  82.     form_process();
  83.  
  84. ?>
  85.  

My form......

Expand|Select|Wrap|Line Numbers
  1. <form id="test" method="post" onsubmit="return false;">
  2.         <table border="0">
  3.                <tr>
  4.                    <td colspan="2">
  5.  
  6.                        <div id="errors">
  7.                                &nbsp;
  8.                        </div>
  9.  
  10.                    </td>
  11.                </tr>
  12.  
  13.                <tr>
  14.                    <td colspan="2">
  15.  
  16.                       <font size="+2"><b>Email Us</b></font>
  17.  
  18.                    </td>
  19.                </tr>
  20.  
  21.                <tr>
  22.                    <td>
  23.                         <b>Name:</b>
  24.  
  25.                         <input type="text" name="name" id="name" size="25" maxlength="20" />
  26.                    </td>
  27.                </tr>
  28.  
  29.                <tr>
  30.                    <td>
  31.                         <b>Email:</b>
  32.  
  33.                         <input type="text" name="email" id="email" size="25" maxlength="35" />
  34.                    </td>
  35.                </tr>
  36.  
  37.                <tr>
  38.                    <td>
  39.                         <b>&nbsp;Message:</b> 
  40.                    </td>
  41.                    <td>
  42.                         <i>(max 250 characters allowed)</i>
  43.                    </td>
  44.                </tr>
  45.                <tr>
  46.                    <td colspan="2">
  47.                         <textarea name="message" id="message" cols="36" rows="10"></textarea>
  48.                    </td>
  49.                </tr>
  50.                <tr>
  51.                    <td colspan="2" align="right">
  52.                         <input type="submit" value="submit" name="submit" onclick="javascript: sendRequest();" />
  53.                    </td>
  54.                </tr>
  55.         </table>
  56.         </form>
  57.  
  58.  
please help
Aug 16 '09 #1
9 2609
labmonkey111
44 New Member
Many things could cause the email not to send, or appear not to send. Are you getting any errors? Does it say its send its but you don't get the email? In anything in the error logs?

A few problems I had when first using the mail() function was that php.ini needed to use the full path to sendmail, the message text needs each line to be less than 70 characters long, and of course, my spam filter kicked it every time. Giving more detail should help us figure out your problem.

I just noticed this line
Expand|Select|Wrap|Line Numbers
  1. <form id="test" method="post" onsubmit="return false;">
Why are you doing a "return false"? That will cause the form not to submit at all, which could be your whole problem right there.
Aug 16 '09 #2
gopan
41 New Member
Dear labmonkey111 ,
luke noob is using a javascript to send the form...
And php mail( ) does not require the path to send mail. i think you thought of perl mail( ).

Dear luke noob,
First of all your sendRequest( ) function is not given in the code... I'm assuming its having a basic validation and submit code (line#52 in form code)

In your mail function php code try the code below at line#53
Expand|Select|Wrap|Line Numbers
  1.         $sent = @mail($to,$subject,$message,$headers);
  2.  
  3.         if ($sent ) {
  4.             echo "Thank you for your inquiry, Your message has been received. <br>We will get back to you as soon as we can.";
  5.         } else {
  6.             echo "Your message could not be sent at this time. Please try again.";
  7.         }
  8.  
coz I encounter not sending mail if my code mail() in a if condition.. dunno the reason why.

You may also check if the mail function fails for real thru

Expand|Select|Wrap|Line Numbers
  1.       mail($to,$subject,$message,$headers) or die("Mail function failed");
  2.  
Try this...
Aug 17 '09 #3
luke noob
69 New Member
Thanks for the reply, i have used a bit of ajax that is not shown here it is
Expand|Select|Wrap|Line Numbers
  1. <style>
  2.         #errors
  3.         {
  4.  
  5.             font-size:14px;
  6.             font-weight:normal;
  7.             margin:0px;
  8.             padding:0px;
  9.         }
  10.     </style>
  11.  
  12.     <script type="text/javascript" src="js/prototype.js"></script>
  13.     <script type="text/javascript">
  14.  
  15.     function sendRequest() {
  16.  
  17.         new Ajax.Request("email_form_process.php", {
  18.                method: 'post',
  19.                postBody: "name="+$F("name")+"&email="+$F("email")+"&message="+$F("message"),
  20.                onComplete: showResponse
  21.  
  22.         });
  23.     }
  24.  
  25.  
  26.     function showResponse(req)
  27.     {
  28.        $('errors').style.borderStyle= "solid";
  29.  
  30.        var res=/message was received/;
  31.  
  32.        if(req.responseText.match(res))
  33.        {
  34.         $('errors').style.borderColor= "green";
  35.         $('errors').style.color="green";
  36.        }else{
  37.         $('errors').style.borderColor= "red";
  38.         $('errors').style.color="red";
  39.        }
  40.  
  41.        $('errors').style.borderSize= "1px";
  42.        $('errors').innerHTML=  req.responseText;
  43.     }
  44.  
  45.     </script>
  46.  
i tryed again today and it works. For some reason it didnt yesterday,

i have another problem though, i have an input feild for the name to be enterd, but when i post it to my email address the name does not show there, i have tryed to edit this to work but its not working still, could you give me some advice please. my input for the name, is "name" but im not sure where to edit my script.
Aug 17 '09 #4
gopan
41 New Member
ooh...
check your first code php script line#49

Expand|Select|Wrap|Line Numbers
  1. $headers = "From: ".$from."\r\n";
  2.  
to

Expand|Select|Wrap|Line Numbers
  1. $headers = "From: ".$name." <".$from.">\r\n";
  2.  
then you will get name of the sender...
Hope this will help...
Aug 17 '09 #5
luke noob
69 New Member
Perfect thank you, I last thing...

Is it possible to sent to two different email addresses at the same time, by changing line 3, somehow

Expand|Select|Wrap|Line Numbers
  1.  
  2. $to = "example@example.co.uk";
  3.  
itz not a serious thing but it would make life a bit easier.
Aug 17 '09 #6
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
I think you can have $to as an array...

Regards
Dheeraj Joshi
Aug 18 '09 #7
gopan
41 New Member
@dheerajjoshim
No dear... php mail function can mail to multiple recipients by giving all the addresses as a coma seperated string... thats all...

Expand|Select|Wrap|Line Numbers
  1.     $to = "example@example.co.uk, example@example.co.au, example@example.co.ar";
  2.  
Aug 18 '09 #8
luke noob
69 New Member
Thank you this helps alot.
Aug 18 '09 #9
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
Thanks for the info Gopan..

I did not know that..


Regards
Dheeraj Joshi
Aug 19 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
3715
by: David Burson | last post by:
Hi, I have a VB.NET windows app that needs to automatically send a simple text email when my users run a new version of the app for the first time. I thought this would be simple, but after days of reading posts and testing, I see it is not - unless I'm missing something? I'm not an email guru. All of my users will be running at least Windows 2000, most on XP sp2. Some of my users will have Outlook, many will be using Outlook...
88
12591
by: Mike | last post by:
Is there a way to determine what a user's default email client is? I read a post from 3 years ago that said no. I guess I'm hoping something has come along since then.
14
9149
by: supz | last post by:
Hi, I use the standard code given below to send an email from an ASP.NET web form. The code executes fine but no Email is sent. All emails get queued in the Inetpub mail queue. I'm using my local default SMTP Server and my from address is a valid Yahoo/Hotmail address. I have configures the local SMTP Server to allow the IP Address 127.0.0.1.
13
1611
by: nigel.t | last post by:
Using linux <?php exec("/bin/tar -cvzf myfile.tgz /home/",$arrayout,$returnval); ?> or perhaps try it on your system and tell me if it does/doesnt and what your linux is? I've also tried
2
1886
by: RAB | last post by:
I have a .aspx page with the following code: <% @Import Namespace="System.Web.Mail" %> <%@ page language="vb" debug="true" runat="server" %> <script runat="server"> Sub Click(sender as Object, e as EventArgs) 'Create an instance of the MailMessage class
1
2569
by: Sand Yaah | last post by:
In my Ajax Code: function selDistServe() { alert("SelDistServe"); url = "http://gistest/newkerala/LinkHtml"; xmlHttp = initRequest(); xmlHttp.onreadystatechange = selDistProcess; xmlHttp.open("GET", url, true);
0
3284
by: glubber | last post by:
I have a cronned perl script that runs on a different server. when I try to restore it to the server below things dont copy across correctly. the target server is described below. debian 4.0 - Linux testbox.jumboentertainment.com.au 2.6.18-4-686 #1 SMP Mon Mar 26 17:17:36 UTC 2007 i686 GNU/Linux with the latest apt-get installation of postgres
4
6155
by: gamesforums | last post by:
Hi! I have some code within the Application_Start event in Global.asax that runs a sqlscript against my sql server to check the databas version. My problem is that sometimes the Application_Start event doesnt fire, it skips it and goes directly to my default.aspx file. I do the following: 1. IISReset.
1
1601
by: Dany13 | last post by:
hi all. i using some text box for input value and some localvarible for passing this data to dataset . give instance for correct row of dataset and data in data table . use one gird view for showing curent data in dataset . in end i am calling update metod to insert data in sql database but this metod doesnt work correctly. at all doesnt work. but givenot any error . fill data from my database (work propebly) ...
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10373
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5683
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.