Viernes, 04 Junio 2010 18:18

Envió de mensajes desde PHP internacionalizado

Valora este artículo
(1 Voto)

Esta pequeña clase en PHP permite usando las librerías de PEAR Mail enviar mensajes de una forma fácil, controlando su envió, adjuntado HTML, vídeos, fotos, etc..

Pero normalmente todos los ejemplo suelen estar en ingles y claro nosotros los Castellano parlantes, necesitamos acentos, eñes o cualquier signo no incluido en el standard US-ASCII

Ver código
  1. <?php
  2.  
  3. require_once 'Mail.php';
  4. require_once 'Mail/mime.php';
  5.  
  6. class send_mail {
  7. var $host;
  8. var $charset;
  9. var $msg;
  10. var $auth;
  11. var $user;
  12. var $password;
  13.  
  14. function send_mail() {
  15. $this->host='localhost';
  16. $this->charset='UTF-8';
  17. $this->msg='';
  18. $this->file=array();
  19. }
  20.  
  21. function host($host) {
  22. $this->host=$host;
  23. }
  24.  
  25. function auth($user, $password) {
  26. $this->auth=true;
  27. $this->user=$user;
  28. $this->password=$password;
  29. }
  30.  
  31. function charet($charset) {
  32. $this->chaset=$charset;
  33. }
  34.  
  35. function Attachment($file) {
  36. array_push($this->file, $file);
  37. }
  38.  
  39. function body($msg) {
  40. $this->msg=htmlentities($msg, ENT_QUOTES, $this->charset);
  41. }
  42.  
  43. function body_html($msg) {
  44. $this->msg=mb_convert_encoding($msg, $this->charset, 'auto');
  45. }
  46.  
  47. function send($from, $to, $subject)
  48. {
  49. $message = new Mail_mime();
  50. $message->setHTMLBody($this->msg);
  51.  
  52. if (! empty($this->file)) {
  53. foreach($this->file as $file) {
  54. $message->addAttachment($file);
  55. }
  56. }
  57.  
  58. $body = $message->get(array('html_charset' => $this->charset));
  59.  
  60. $subject = mb_convert_encoding($subject, 'ISO-8859-1', 'auto');
  61. $extraheaders = array("From" => $from, "Subject" => mb_encode_mimeheader($subject, 'ISO-8859-1', 'Q'), "Date" => date("D , d M Y H:i:s O"));
  62. $headers = $message->headers($extraheaders);
  63.  
  64. $mail = Mail::factory("smtp", array("host" => $this->host, "auth" => $this->auth, "username" => $this->user, "password" => $this->password));
  65. return $mail->send($to, $headers, $body);
  66. }
  67. }
  68.  
  69. ?>
  70.  

icon mail pear Version 1.3 (1.63 kB 2010-10-23 21:51:04)

 

Código de ejemplo:

Ver código
  1. <?php
  2.  
  3. // Abrimos la clase
  4. $mail = new send_mail;
  5.  
  6.  
  7. // Adjuntamos el archivo
  8. $mail->Attachment('ventas.pdf');
  9.  
  10. // Insertamos el cuerpo del mensaje
  11. $mail->body('Lararala tralara');
  12.  
  13. // Enviamos el mensaje
  14. $res=$mail->send('Esta dirección de correo electrónico está siendo protegida contra los robots de spam. Necesita tener JavaScript habilitado para poder verlo.', 'Esta dirección de correo electrónico está siendo protegida contra los robots de spam. Necesita tener JavaScript habilitado para poder verlo.', 'Informe de ventas');
  15.  
  16. // Comprobamos el resultado
  17. if (PEAR::isError($res)) {
  18. echo "Error en el envió";
  19. }
  20.  
  21. ?>

 

Referencias:

 

Leer 5916 veces Modificado por última vez en Viernes, 06 Julio 2012 12:26

Escribir un comentario


Código de seguridad
Refescar