image


Valora:  3/5
Inicio » PHP » PHP Tutorial » Processing forms




Sending emails

PHP offers the possibility to send email in a simple easy way, to that end the language provides us with the statement mail()

<?php
   mail(receiver, subject, message text);
?>

In the receiver parameter we will write the email address where the message will be sent to; in the parameter subject, the message subject; and in the parameter message text, the message body in plain text format.

There is an extended syntax of the mail() statement that allows us to include additional information to the header of the message.

<?php
   mail(receiver, subject, message text, additional parameters);   
?>

In the header information we can include additional parameters to the message such as Reply-To:, From:, Content-type:… that allow us to have a greater control over the message

<!-- PHP Tutorial WebEstilo.com -->
<html>
<head>
   <title>PHP Example</title>
</head>
<body>
<H1>Sending emails</H1>
Insert your email:
<FORM ACTION="email.phtml" METHOD="GET">
<INPUT TYPE="text" NAME="direccion"><BR><BR>
Format: <BR>
<INPUT TYPE="radio" NAME="tipo" VALUE="plano" CHECKED> Text plain<BR>
<INPUT TYPE="radio" NAME="tipo" VALUE="html"> HTML<BR><BR>
<INPUT TYPE="submit" VALUE="Enviar">
</FORM>
</body>
</html>

View code


email.phtml
<!-- PHP Tutorial WebEstilo.com -->
<html>
<head>
   <title>PHP Example</title>
</head>
<body>
<H1>Ejemplo de envio de email</H1>
<?
   $direccion=$_GET['direccion'];
   $tipo=$_GET['tipo'];
   
   if ($direccion!=""){
   if ($tipo=="plano"){
      // Envio en formato texto plano
      
      mail($direccion,"Ejemplo de envio de email","Ejemplo de envio de email de texto plano\n\nWebEstilo.\nhttp://www.webestilo.com/\n Manuales para desarrolladores web.\n","FROM: Pruebas <webmaster@hotmail.com>\n");
   } else {
      // Envio en formato HTML
      mail($direccion,"Ejemplo de envio de email","<html><head><title>WebEstilo. Manual de PHP</title></head><body>Ejemplo de envio de email de HTML<br><br>WebEstilo.<br>http://www.webestilo.com/<br> <u>Manuales</u> para <b>desarrolladores</b> web.</body></html>","Content-type: text/html\n", "FROM: Pruebas <webmaster@hotmail.com>\n");
   }      
echo "Se ha enviado un email a la direccion: ",$direccion," en formato <b>",$tipo,"</b>.";
}
?>

<br>
</FORM>
</body>
</html>

View code





WebEstilo.com - Introduzca su e-mail y conozca las novedades. No hacemos Spam.
Google
  Web WebEstilo.com   
 
Valid HTML 4.01!
Última modificación:25 de Diciembre de 2004. Spain - España.
© 1998-2004 por Joaquin Gracia. Todos los derechos reservados.