www.webdeveloper.com
Results 1 to 10 of 10

Thread: Help with PHP Mail function

  1. #1
    Join Date
    Mar 2013
    Posts
    6

    Smile Help with PHP Mail function

    Hi, I'm new to the forum and new to the php world.

    My boss asked me if I could add a Mail feature to our Contact page (coded in HTML)

    He gave me the FTP connection, I downloaded the "public_html" folder and found my first problem... I don't know which IDE I should open the project with (I usually use Eclipse for my Java projects).

    1- What IDE should I use? I downloaded Dreamweaver but I don't know if that's the right one.

    So I started looking at the code, and as of now it looks like this (CONTACT.HTML):

    <div id="contactform">
    <form id="contact" action="js/process.php" method="post">
    <fieldset>
    <div class="one_half firstcols">
    <label for="name" id="name_label">Name <span class="required">(required)</span></label>
    <input type="text" name="name" id="name" value="" class="text-input" size="40" />
    </div>

    <div class="one_half lastcols">
    <label for="email" id="email_label">E-mail <span class="required">(required)</span></label>
    <input type="text" name="email" id="email" value="" class="text-input" size="40" />
    </div>

    <div class="one_half firstcols">
    <label for="website" id="website_label">Sitio Web</label>
    <input type="text" name="website" id="website" value="" class="text-input" size="40" />
    </div>

    <div class="one_half lastcols">
    <label for="subject" id="subject_label">Subject</label>
    <input type="text" name="subject" id="subject" value="" class="text-input" size="40" />
    </div>

    <div class="clear"></div>

    <label for="msg" id="msg_label">Message <span class="required">(required)</span></label>
    <textarea cols="60" rows="15" name="msg" id="msg" ></textarea> <br class="clear" />
    <input type="submit" name="submit" class="butcontact" id="submit_btn" value="Enviar"/>
    </fieldset>
    </form>
    <br class="clear" />
    <span class="error" id="name_error">Please input your name !</span>
    <span class="error" id="email_error">Please input your mail !</span>
    <span class="error" id="email_error2">Please use a valid !</span>
    <span class="error" id="msg_error">Por Favor completa tu mensaje !</span>
    </div>


    And this is js/process.php:

    <?php

    /* Recovering params */

    $name = $_POST["name"];
    $mail= $_POST["email"];
    $site = $_POST["website"];
    $message = $_POST["msg"];
    $subject = $_POST["subject"];

    /* Define destination */

    $destination = "jgarrido@opticomsa.com.ar";

    /* Define message */

    $body = "<strong>Name: </strong>".$name."<br />
    <strong>Mail: </strong>".$mail."<br />
    <strong>Web Site: </strong>".$site."<br />
    <strong>Subject: </strong>".$subject."<br />
    <strong>Message: </strong>".$message;

    /* Define header */

    $header= "MIME-Version: 1.0\r\n";
    $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
    $header .= "From: ".$mail."\r\n";
    $header .= "Reply-to: ".$mail."\r\n";
    $header .= "Cc: ".$mail."\r\n";

    /* Sending E-Mail */

    if(mail($destination, $subject, $body, $header)) {
    echo 'Your message has been sent';
    }
    else {
    echo 'Please try again';
    }

    ?>


    2- This does absolutely nothing. What should I change to make it work?

    Thanks!

  2. #2
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    Your HTML is wrong. Your form element had no closing tag, neither did the fieldset or the container div. I've taken the liberty of correcting the errors and your code worked in my test:

    Code:
    <div id="contactform">
    	<form id="contact" action="js/process.php" method="post">
    		<fieldset>
    
    		<div class="one_half firstcols">
    			<label for="name" id="name_label">Name <span class="required">(required)</span></label>
    			<input type="text" name="name" id="name" value="" class="text-input" size="40" />
    		</div>
    
    		<div class="one_half lastcols">
    			<label for="email" id="email_label">E-mail <span class="required">(required)</span></label>
    			<input type="text" name="email" id="email" value="" class="text-input" size="40" />
    		</div>
    
    		<div class="one_half firstcols">
    			<label for="website" id="website_label">Sitio Web</label>
    			<input type="text" name="website" id="website" value="" class="text-input" size="40" />
    		</div>
    
    		<div class="one_half lastcols">
    			<label for="subject" id="subject_label">Subject</label>
    			<input type="text" name="subject" id="subject" value="" class="text-input" size="40" />
    		</div>
    
    		<input type="submit" />
    
    		</fieldset>
    	</form>
    
    	<div class="clear"></div>
    </div>
    With respect to the editor best suited to PHP, that's entirely at your discretion. I use Notepad++ to write all my code in, but some people use plain old notepad. Dreamweaver is an option, but it's not a good IDE in my opinion. It has a heavy, sluggish feeling to me and it also has annoying features that bug me such as auto code completion (which I know can be switched off) and annoying pop-up boxes that get right in the way of your code as your writing it. Dreamweaver is more of a designers tool to knock up websites without having to know too much about HTML/CSS.

    N.B. please try using code tags :-).
    Last edited by George88; 03-14-2013 at 04:47 PM.

  3. #3
    Join Date
    Mar 2013
    Posts
    6
    Hi, thanks for your reply, I'll try to edit and add code tags right away.

    The form, fieldset and div are closed on the html I posted. I received the mail you sent, can you tell me what else may be wrong?

    This is where I close the tags:

    HTML Code:
    </fieldset>
    </form>
    <br class="clear" />
    <span class="error" id="name_error">Please input your name !</span>
    <span class="error" id="email_error">Please input your mail !</span>
    <span class="error" id="email_error2">Please use a valid !</span>
    <span class="error" id="msg_error">Por Favor completa tu mensaje !</span>
    </div>

  4. #4
    Join Date
    Mar 2013
    Posts
    6
    This is my HTML head, do I have to include something else for the function to work?

    HTML Code:
    <head>
    	<link rel="shortcut icon" href="images/icono.jpg" />
    	<meta charset="utf-8" />
    	<title>Opticom</title>
    	<link href="styles/reset.css" type="text/css" rel="stylesheet" />
    	<link href="styles/style.css" type="text/css" rel="stylesheet" />
    	<link href="styles/inner.css" rel="stylesheet" type="text/css" />
    	<!--[if IE]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
    	<!-- ////////////////////////////////// -->
    	<!-- //      Javascript Files        // -->
    	<!-- ////////////////////////////////// -->
    	<script type="text/javascript" src="js/jquery-1.6.4.min.js"></script>
    	<script type="text/javascript" src="js/hoverIntent.js"></script> 
    	<script type="text/javascript" src="js/superfish.js"></script> 
    	<script type="text/javascript" src="js/supersubs.js"></script>
    	<script type="text/javascript" src="js/contact.js"></script>
    	<script type="text/javascript" src="js/process.php"></script>
    	<script type="text/javascript">
    	jQuery(document).ready(function(){
    		//=================================== MAIN MENU ===================================//
    		jQuery("ul.sf-menu").supersubs({ 
    		minWidth		: 12,		// requires em unit.
    		maxWidth		: 14,		// requires em unit.
    		extraWidth		: 3	// extra width can ensure lines don't sometimes turn over due to slight browser differences in how they round-off values
    							   // due to slight rounding differences and font-family 
    		}).superfish();  // call supersubs first, then superfish, so that subs are 
    						 // not display:none when measuring. Call before initialising 
    						 // containing tabs for same reason.
    						 
    	});
    	</script>
    </head>

  5. #5
    Join Date
    Mar 2013
    Posts
    6
    George can you tell me how you test your code once you finished writing it?

    Do you use some special folder structure? (the web i'm currently working has a root "public_html" and everything else is inside that folder)

    What server do I need to download for testing?

  6. #6
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    Hi there,

    Sorry about that. I was in a rush to leave for work when I tested your code and didn't copy & paste it properly. Consider my first message void. Upon further inspection, I actually don't see any problems with your code. I've tested it a number of times now and it works as expected.

    In your first message you said it does absolutely nothing, but didn't elaborate on this. When you click the submit button, does your browser get redirected to js/process.php? This would be expected behaviour. If it does redirect, does your code display your warning message, or simply do nothing at all? If it does nothing, this would suggest the function executes correctly.

    Consider writing a test script like this:

    PHP Code:
    mail('jgarrido@opticomsa.com.ar''Test subject''Message') or die('Error'); 
    If the mail function still does not work, it could be a problem with your web hosting. At this point you would need to contact the host and ask them if SMTP is supported, since they may not have it installed, though this is unlikely.

    Regarding your questions about testing code, I just save the files and upload them to a folder on my web server where I run them. No special folder structure is necessary. Most files reside in public_html or www.
    Last edited by George88; 03-15-2013 at 01:00 PM.

  7. #7
    Join Date
    Mar 2013
    Posts
    6
    Hi, thanks again for your help.

    When I click the submit button, I don't get redirected, nor I get any error message or anything. It just stays there.

    I'll try testing the code again in a local environment and see if I can make it work.

    If you have any additional comments they are very welcome!

    I'll come back with the results later.

    Thanks again

  8. #8
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    It may be a long shot, but I've had issues with submit buttons in the past where I've given the name, type, id and other attributes the same name (all "submit") and the functionality of the button was disabled. Incidentally, the submit button doesn't need a name attribute.

    It probably won't work, but try changing:

    PHP Code:
    <input type="submit" name="submit" class="butcontact" id="submit_btn" value="Enviar"/> 
    To something like:

    PHP Code:
    <input type="submit" class="butcontact" id="btn" value="Enviar"/> 

  9. #9
    Join Date
    Mar 2013
    Posts
    6
    I tried testing it locally but now it redirects to js/process.php and shows me the code on that file.

    I changed the submit properties on the website but it keeps doing absolutely nothing.

    Is it ok my "contact" page extension is .html? Or should I change it to .php?

  10. #10
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    It should make no difference what the file extension is. In cases where the problem is not forthcoming, I like to isolate the code in question and test it externally. Try copying just the code you need to test into a new file, along with your PHP handler in another, and see if it still does nothing.

    If it works, you can assume something else in your source file(s) is affecting the functionality of the button. If it still does not work, I don't know what that could be.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles