Hola, estoy aprendiendo un poco php, y he modificado
un formulario en HTML5 que me ha quedado así:
Código HTML:
Ver original<form method="post" action="enviar.php">
<!-- <input type="hidden" name="asuntooculto" value="asuntooculto"> -->
<!-- no funciona $subject = jayj_html5_sanitize_string( $_REQUEST['asuntooculto'] ); -->
<!-- tampoco funciona if ( mail( $email, "$subject (Asunto personalizado)", -->
<label for="nombre2">Nombre:
</label><input type="text" name="nombre" id="nombre2" value=""><br> <label for="email2">Email*:
</label><input type="text" name="email" id="email2" value=""><br><br> <label for="subject2">Asunto:
</label><input type="text" name="subject" id="subject2" value=""><br><br>
<label for="texto2">Texto 1:
</label><input type="text" name="texto-primero" id="texto2" value=""><br> <label for="texto3">Texto 2:
</label><input type="text" name="texto-segundo" id="texto3" value=""><br><br>
<label for="fecha2">Fecha start:
</label><input type="date" name="fecha-primero" id="fecha2"><br> <label for="fecha3">Fecha end:
</label><input type="date" name="fecha-segundo" id="fecha3"><br> <label for="numero2">Numero:
</label><input type="text" name="numero" id="numero2" value=""><br>
<button type="submit" value="submit">Enviar formulario
</button>
Pasa que al querer extraer las variables con enviar.php, sólo me funciona el envío del mail con la sentencia original o agregado una sola variable más. Luego aparentemente, agregado una sola variable una por una, funciona con todas, pero no juntas. La sentencia original es:
Código PHP:
if ( mail( $email, "$subject", utf8_decode( $message ), "From: $name < $sender>" ) )
y no con la cadena completa como quiero:
Código PHP:
if ( mail( $email, "$subject", $texto1, $texto2, $menuselect, $fechastart, $fechaend, $numero, utf8_decode( $message ), "From: $name < $sender>" ) )
El Error es Warning: mail() expects at most 5 parameters, 10 given in .../enviar.php on line 90 (es decir la línea de arriba)
Además, querría poner un asunto predefinido y esconder el campo Asunto en el formulario, pero no hay manera, he intentado con $_REQUEST['asuntooculto'] estando el input del asunto hidden, como pueden ver en el archivo enviar.php:
Código PHP:
<?php
/**
* PHP contact form
*/
// If the form have been submitted and the spam check field is empty
if ( isset( $_POST['email'] ) && empty( $_POST['s_check'] ) ) :
/**
* Enter your email here
*/
$email = '[email protected]';
/**
* Language strings
*/
$strings = array(
'default_subject' => 'Message on your website',
'default_name' => 'Anonymous',
'error_message' => '<strong>Error:</strong> %s cannot be left blank', // %s is the error name
'invalid_email' => '<strong>Error:</strong> Email address is invalid',
'email_success' => 'Gracias! hemos recibido su mensaje',
'email_error' => 'There was a problem sending your email. Please try again'
);
/**
* Required fields
* We'll check and see if any of the required fields are empty.
*/
$required = array(
//'name' => 'Name',
//'subject' => 'Subject',
'email' => 'Email',
'mensaje' => 'Message'
);
// Declare our $errors variable we will be using later to store any errors.
$errors = array();
/**
* Get form values and sanitize them
*/
$name = jayj_html5_sanitize_string( $_POST['nombre'] );
$from = filter_var( $_POST['email'], FILTER_SANITIZE_EMAIL );
$subject = jayj_html5_sanitize_string( $_POST['subject'] );
//$subject = jayj_html5_sanitize_string( $_REQUEST['asuntooculto'] );
//$subject = 'Asunto personalizado tampoco funciona';
$texto1 = jayj_html5_sanitize_string( $_POST['texto-primero'] );
$texto2 = jayj_html5_sanitize_string( $_POST['texto-segundo'] );
$menuselect = jayj_html5_sanitize_string( $_POST['menuselect'] );
$fechastart = jayj_html5_sanitize_string( $_POST['fecha-primero'] );
$fechaend = jayj_html5_sanitize_string( $_POST['fecha-segundo'] );
$numero = jayj_html5_sanitize_string( $_POST['numero'] );
$message = jayj_html5_sanitize_string( $_POST['mensaje'] );
$sender = '[email protected]'; // or another email from your own domain
$message .= "\n Sent from: $from";
// Validate the sanitized email
if ( ! filter_var( $from, FILTER_VALIDATE_EMAIL ) )
$errors[] = $strings['invalid_email'];
// Set a default name
if ( empty( $name ) )
$name = $strings['default_name'];
// Set a default subject
if ( empty( $subject ) )
$subject = $strings['default_subject'];
/**
* Loops through each required $_POST value
* Checks to ensure it is not empty.
*/
foreach ( $required as $key => $value ) :
if ( isset( $_POST[$key] ) && ! empty( $_POST[$key] ) )
continue;
else
$errors[] = sprintf( $strings['error_message'], $value );
endforeach;
/**
* Now check to see if there are any errors
*/
if ( empty( $errors ) ) {
// No errors, send mail using conditional to ensure it was sent.
if ( mail( $email, "$subject", $texto1, $texto2, $menuselect, $fechastart, $fechaend, $numero, utf8_decode( $message ), "From: $name < $sender>" ) )
echo '<p class="alert alert-success">' . $strings['email_success'] . '</p>';
else
echo '<p class="alert alert-error">' . $strings['email_error'] . '</p>';
} else {
// Errors were found, output all errors to the user.
echo '<div class="alert alert-warning">';
echo implode( '<br />', $errors );
echo '</div>';
}
else :
// The user have tried to access thid page directly or is a spambot
die("You're not allowed to access this page directly");
endif;
/**
* Sanitize inputs
*
* @uses FILTER_SANITIZE_STRING Strip tags
* @uses FILTER_FLAG_NO_ENCODE_QUOTES Prevents encoding of quotes
* @uses stripslashes() Removes slashes added to quotes
*
* @since Jayj HTML5 theme 2.1
* @param string $string The string to be sanitized
* @return string The sanitized string
*/
function jayj_html5_sanitize_string( $string ) {
return stripslashes( filter_var( $string, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES ) );
}
A ver si me podrían ayudar a recibir todos los parámetros por email y a poner un asunto personalizado quitando el input asunto. Gracias de antemano.