rulu ruru

postClase para mandar email

November 25th, 2007

Filed under: PHP — admin @ 3:43 pm

Estaba al pedo, ya que estos días ando al pedo, el doctor dijo que debia tomar un descanzo por el estres, pero no puedo así que me pongo a codear cualquier cosa.

Hoy hice una clase que sirve para formularios de contacto principalmente.

El uso de la clase es simple, se instancia con el remitente el titulo, y luego se introduce el texto del body, al introducirlo la clase lo separa en parrafos (<p></p>) y si el campo tiene saltos de linea “\n” lo cambia automaticamente a “<br>”
Una vez que tenemos el texto que irá en el body, ponemos la dirección de correo electronico a donde será enviado el email y le damos “enviar”, estas dos últimas devuelven true o false.

Les dejo la clase, luego pongo un ejemplo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?
class email {
var $Temail;
var $titulo;
 
function __construct($Temail,$titulo) {
if ($this->comprobar_email($Temail) == 'no') { echo "Email de envio incorrecto"; die; } 
if (!ereg("^[a-zA0-Z9]+$",$titulo)) { echo "Titulo incorrecto"; die; }  
 
$this->titulo = $titulo;
$this->remitente = $Temail;
$this->head = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>'; 
$this->cabecera = "From: ".$Temail."\r\nContent-type: text/html\r\n";
$this->footer = '</body></html>';
}
 
function a($a) { 
if ($this->comprobar_email($a) == 'no') { return false; } 
$this->envio = $a;
return true;
}
 
function insert($a) { 
$this->cuerpo .= "<p>".nl2br($a)."</p>";
}
 
function enviar() { 
$this->all = $this->head.$this->cuerpo.$this->footer;
if (mail($this->envio,$this->titulo,$this->all,$this->cabecera)) { return true; } return false; 
}
 
// Esta clase no me pertenece
function comprobar_email($email){
$mail_correcto = 0;
if ((strlen($email) >= 6) && (substr_count($email,"@") == 1) && (substr($email,0,1) != "@") && (substr($email,strlen($email)-1,1) != "@")){
if ((!strstr($email,"'")) && (!strstr($email,"\"")) && (!strstr($email,"\\")) && (!strstr($email,"\$")) && (!strstr($email," "))) {
if (substr_count($email,".")>= 1){
$term_dom = substr(strrchr ($email, '.'),1);
if (strlen($term_dom)>1 && strlen($term_dom)<5 && (!strstr($term_dom,"@")) ){
$antes_dom = substr($email,0,strlen($email) - strlen($term_dom) - 1);
$caracter_ult = substr($antes_dom,strlen($antes_dom)-1,1);
if ($caracter_ult != "@" && $caracter_ult != "."){
$mail_correcto = 1;
}
}
}
}
}
if ($mail_correcto)
return 'si';
else { 
return 'no';
}
} 
// termina clase de comprobar email.
 
}
 
$enviando = new email("tuemail@tudominio.com","InvisionArg Php code");
$enviando->insert("Le recordamos que está utilizando una clase realizada por invisionarg");
$enviando->insert('<font color="#CC0000"><b>Este es un texto de prueba
Ud puede modificarlo cuando quiera.</b></font>');
if (!$enviando->a("emaildestino@domaindestino.com")) { echo "El email de envio es incorrecto"; }
if ($enviando->enviar()) { echo "email enviado"; } else { "email no enviado"; }
?>
Enlaces pagos


No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.

ruldrurd
Entries (RSS) and Comments (RSS)