Problema formular de contact nu trimite mesaje

Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
R32Alin
Mesaje: 27

Problema formular de contact nu trimite mesaje

am acest formular de contact in html si formularul de contact in php sendmail.php insa nu trimite mesaje.
cand dau sa trimita spune ca asteapta dupa care nu trimite si ramane mesajul
pana a schimba hostingul care nu aveam php trimitea dar dea erorile de la php. bineinteles ca nu ajungea emailul la destinatie.

Cod: Selectaţi tot

<form class="contact-form" method="post" action="sendmail.php" enctype="multipart/form-data">
          <!-- The feedback message is here -->
          <div class="usermessagea"></div>
          <fieldset>
            <ul>
              <li class="text-field">
                <label for="name-main"> <span class="label">What's your <span class="highlight-text">name</span>?</span> </label>
                <input type="text" name="name" id="name-form" class="required" value="">
                <div class="msg-error"></div>
              </li>
              <li class="text-field">
                <label for="email-main"> <span class="label">What's your <span class="highlight-text">e-mail</span>?</span> </label>
                <input type="text" name="email" id="email-form" class="required email-validate" value="">
                <div class="msg-error"></div>
              </li>
              <li class="textarea-field">
                <label for="message-main"> <span class="label">How can I <span class="highlight-text">help</span> you?</span> </label>
                <textarea name="message" id="message-form" rows="8" cols="30" class="required"></textarea>
                <div class="msg-error"></div>
              </li>
              <li class="submit-button">
                <input type="hidden" name="action" value="sendmail.php" id="action">
                <input type="submit" name="sendmail" value="send message" class="sendmail alignleft">
              </li>
            </ul>
          </fieldset>
        </form>
        <script>
                            // specif here the message for each field of contact form, by ID of field
                        	var error_messages = {
                        		name: "It's required a valid name.",
                        		email: "Write a valid email.",
                        		message: "Insert the message."
                        	};
                        </script>
      </div>
iar aici codul php din sendmail.php

Cod: Selectaţi tot

<?php
/**
 * Define the from email
 */ 
 
// email
define('TO_EMAIL', 'support@stefanelc.com'); 
define('FROM_EMAIL', 'support@stefanelc.com');  
define('FROM_NAME', 'Test'); 

/**
 * define the body of the email. You can add some shortcode, with this format: %ID%
 * 
 * ID = the id have you insert on the html markup.
 * 
 * e.g.
 * <input type="text" name="email" />
 *       
 * You can add on BODY, this:
 * email: %email%   
 */ 
define( 'BODY', '%message%<br /><br /><small>email inviata da %name%, email %email%.</small>' );
define( 'SUBJECT', 'Email from yoursite.com' );

// here the redirect, when the form is submitted
define( 'ERROR_URL', 'contatti_error.html' );
define( 'SUCCESS_URL', 'contatti_success.html' ); 
define( 'NOTSENT_URL', 'contatti_notsent.html' );           

// the message feedback of ajax request
$msg = array(
    'error' => '<p class="error">Warning! Fill correctly the fields marked in red</p>',
    'success' => '<p class="success">Email sent correctly. Thanks to get in touch us!</p>',
    'not-sent' => '<p class="error">An error has been encountered. Please try again.</p>'
);      
    
// the field required, by name
$required = array( 'name', 'email', 'message' );

/**
 * Send the email.
 * 
 * SERVER-SIDE: the functions redirect to some URL, in base of result of control and send.
 * The urls must be set in the constants above: ERROR_URL, SUCCESS_URL, NOTSENT_URL
 * 
 * CLIENT-SIDE: in js/contact.js, there is already script for real-time checking of fields
 * and for ajax request of send email, that request in this page (sendmail.php) and echo the feedback message.    
 */   
sendemail();
    
// NO NEED EDIT
function sendemail() {
    global $msg, $required;
    
    if ( isset( $_POST['ajax'] ) ) $ajax = $_POST['ajax'];
    else $ajax = false;
    
    if ( isset( $_POST['action'] ) AND $_POST['action'] == 'sendmail' ) {
        $body = BODY;
        
        $post_data = array_map( 'stripslashes', $_POST );
        
//         print_r($post_data);
//         die;
        
        foreach ( $required as $id_field ) {
            if( $post_data[$id_field] == '' || is_null( $post_data[$id_field] ) ) {
                if ( $ajax ) end_ajax( $msg['error'] );
                else redirect( ERROR_URL );
            }                       
        }
        
        if( !is_email( $post_data['email'] ) OR $post_data['email'] == '' ) 
            if ( $ajax ) end_ajax( $msg['error'] );
            else redirect( ERROR_URL );
        
        foreach( $post_data as $id => $var ) {
            if( $id == 'message' ) $var = nl2br($var);
            $body = str_replace( "%$id%", $var, $body );    
        }
        
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
        $headers .= 'From: '.FROM_NAME.' <'.FROM_EMAIL.'>' . "\r\n" . 'Reply-To: ' . $post_data['email'];
    
        $sendmail = mail( TO_EMAIL, SUBJECT, $body, $headers );
             
        if ( $sendmail ) 
            if ( $ajax ) end_ajax( $msg['success'] );
            else redirect( SUCCESS_URL );
        else
            if ( $ajax )  end_ajax( $msg['not-sent'] );
            else  redirect( NOTSENT_URL );
    } 
}

function is_email($email) {
    if (!preg_match("/[a-z0-9][_.a-z0-9-]+@([a-z0-9][0-9a-z-]+.)+([a-z]{2,4})/" , $email)) return false;
    else return true;
}             

function end_ajax( $msg = '' ) {
    echo $msg;
    die;
}           

function redirect( $redirect = '' ) {
    header( 'Location: ' . $redirect );
    die;
} 

MarPlo Mesaje: 4343
Salut
Incearca acest cod in sendmail.php:

Cod: Selectaţi tot

<?php
// email
define('TO_EMAIL', 'support@stefanelc.com'); 
define('FROM_EMAIL', 'support@stefanelc.com');  
define('FROM_NAME', 'Test'); 

/**
 * define the body of the email. You can add some shortcode, with this format: %ID%
 * 
 * ID = the id have you insert on the html markup.
 * 
 * e.g.
 * <input type="text" name="email" />
 *       
 * You can add on BODY, this:
 * email: %email%   
 */ 
define('BODY', '%message%<br /><br /><small>email inviata da %name%, email %email%.</small>');
define('SUBJECT', 'Email from yoursite.com');

// here the redirect, when the form is submitted
define('ERROR_URL', 'contatti_error.html');
define('SUCCESS_URL', 'contatti_success.html'); 
define('NOTSENT_URL', 'contatti_notsent.html');           

// the message feedback of ajax request
$msg = array(
  'error' =>'<p class="error">Warning! Fill correctly the fields marked in red</p>',
  'success' =>'<p class="success">Email sent correctly. Thanks to get in touch us!</p>',
  'not-sent' =>'<p class="error">An error has been encountered. Please try again.</p>'
);      
    
// the field required, by name
$required = array('name', 'email', 'message');

/**
 * Send the email.
 * 
 * SERVER-SIDE: the functions redirect to some URL, in base of result of control and send.
 * The urls must be set in the constants above: ERROR_URL, SUCCESS_URL, NOTSENT_URL
 * 
 * CLIENT-SIDE: in js/contact.js, there is already script for real-time checking of fields
 * and for ajax request of send email, that request in this page (sendmail.php) and echo the feedback message.    
 */
    
// NO NEED EDIT
function end_ajax($msg =''){
  echo $msg;
  die;
}

function redirect($redirect ='') {
  header('Location: '. $redirect);
  die;
}
function sendemail(){
  global $msg, $required;

  if(isset($_POST['ajax'])) $ajax = $_POST['ajax'];
  else $ajax = false;

  if(isset($_POST['action']) && $_POST['action'] =='sendmail.php'){
    $body = BODY;
    $post_data = array_map('stripslashes', $_POST);
      
// print_r($post_data);  die;

    foreach($required as $id_field){
      if($post_data[$id_field] == '' || is_null($post_data[$id_field])){
        if($ajax) end_ajax($msg['error']);
        else redirect(ERROR_URL);
      }
    }

    if(!is_email($post_data['email']) || $post_data['email'] ==''){
      if($ajax) end_ajax($msg['error']);
      else redirect(ERROR_URL);
    }
    foreach($post_data as $id => $var){
      if($id == 'message') $var = nl2br($var);
      $body = str_replace("%$id%", $var, $body);  
    }

    $headers  = 'MIME-Version: 1.0' . PHP_EOL;
    $headers .= 'Content-type: text/html; charset=UTF-8' . PHP_EOL;
    $headers .= 'From: '.FROM_NAME.' <'.FROM_EMAIL.'>' . PHP_EOL . 'Reply-To: ' . $post_data['email'];
  
    $sendmail = mail(TO_EMAIL, SUBJECT, $body, $headers);

    if ($sendmail){
      if ($ajax) end_ajax($msg['success']);
      else redirect(SUCCESS_URL);
     }
    else{
      if ($ajax) end_ajax($msg['not-sent']);
      else redirect(NOTSENT_URL);
    }
  }
}

function is_email($email){
  if(!preg_match("/[a-z0-9][_.a-z0-9-]+@([a-z0-9][0-9a-z-]+.)+([a-z]{2,4})/" , $email)) return false;
  else return true;
}             

sendemail(); 

R32Alin Mesaje: 27
acum spune ca emailul este trimis corect Email sent correctly. Thanks to get in touch us!
dar nu despare din formular cea ce am scris
cum fac sa despara si acel lucru?

MarPlo Mesaje: 4343
Presupun ca folosesti scriptul cu ajax. In cazul asta adaugi in functia ajax, unde e primit raspunsul de la server, acest cod:

Cod: Selectaţi tot

document.getElementById('name-form').value ='';
document.getElementById('email-form').value ='';
document.getElementById('message-form').value ='';

R32Alin Mesaje: 27
si unde adaug acest cod? in fisierul de contact unde exact sau in fisierul sendmail unde anume

MarPlo Mesaje: 4343
Acolo in script unde e codul javascript cu functia ajax. Nu e in ce ai postat.
E scriptul tau, iar programarea nu e Copy-Paste; trebuie sa cunosti tot ce e in el, altfel daca are coduri care iti afecteaza site-ul.

R32Alin Mesaje: 27
la directorul js am fisierul contact.js

Cod: Selectaţi tot

jQuery(document).ready(function($){
    var error = true;  
    
    function addLoading( e ){
		e.val( 'attendi...' ).attr('disabled', true);
	}    
    
    function removeLoading( e, value_submit ) {
		e.val(value_submit).attr('disabled', false);  
	}
	
	function addError(msg, e, effect)
	{
		error = true;        
		e.removeClass('icon success');
		e.addClass('icon error'); 
		e.parent().find('.msg-error').text(msg);	            
		if( effect !== undefined && effect == true )
		{
			e.css({position:'relative'}).animate({left:-10}, 100).animate({left:10}, 100).animate({left:-5}, 100).animate({left:5}, 100).animate({left:0}, 100);
		}
	}                 
	
	function addSuccess(e)
	{                                     
		e.parents('li').addClass('icon success');	
	}
	
	function removeError(e)
	{
		error = false;        
		e.parent().find('.msg-error').text('');     
		e.removeClass('icon error');
		addSuccess(e);
	}          
	
	function checkRequired(e) {
        var name = $(e).attr('name');	
        jQuery.globalEval( 'var msg = error_messages.'+name ); 
        
		if( e.val() == '' )
			addError( msg, e );       
		else               
			removeError(e);    
    }     
	
	function checkEmail(e) {
        var expr = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;     
        var name = $(e).attr('name');
        jQuery.globalEval( 'var msg = error_messages.'+name );	 
		
		if( ( e.val() != '' && !expr.test( e.val() ) ) || ( e.is('.required') && e.val() == '' ) )  
			addError( msg, e );            
		else 
			removeError(e);   
    }
    	
    $('.contact-form').each(function(){
        
        var form = $(this);
        var value_submit = $('.sendmail', form).val();
    
    	$('.required', form).each(function(){  
            $(this).blur(function(){      
                checkRequired( $(this) );    
    		});
    	});                
    	
    	$('.email-validate, form').each(function(){    
            $(this).blur(function(){                
        		checkEmail( $(this) );
    		});
    	});    
        
    	$(form).submit(function(){
    		addLoading( $('input:submit', form) );      
            
            $('input, select, textarea', form).each(function(){
                if ( $(this).hasClass('required') ) checkRequired( $(this) );
                    
                if ( $(this).hasClass('email-validate') ) checkEmail( $(this) );
            });    
             
    		$.post('sendmail.php', $(this).serialize()+'&ajax=1', function(msg) {
                showMessage = function() { $('.usermessagea', form).css({opacity:0}).animate({opacity:1}, 500).html(msg); }      
            
                if ( form.is('#quick-form-footer') ) {
                    $('ul', form).slideUp(500, function(){	
                        showMessage();
    					$( '<a href="#"> Close</a>' ).appendTo( $('.usermessagea', form).find('p') ).addClass('close_msg');
    				});        
                } else showMessage();                 
                  
    		    removeLoading( $('.sendmail', form), value_submit );            
            });                  
            return false;
    	});     
    	
    	$('.close_msg').live( 'click', function(){                
    		$(this).parents('form').find('ul').slideDown(500);
    		$(this).parent().remove();
    		return false;
    	});
    }); 
});

MarPlo Mesaje: 4343
Adauga acel cod in functia removeLoading(). Sa fie asa:

Cod: Selectaţi tot

function removeLoading(e, value_submit){
  document.getElementById('name-form').value ='';
  document.getElementById('email-form').value ='';
  document.getElementById('message-form').value ='';
  e.val(value_submit).attr('disabled', false); 
}

R32Alin Mesaje: 27
problema rezolvata ms mult de tot

Subiecte similare