CentOS Php Mail SMTP script

La idea es hacer un script que utilice una cuenta de correo y su password y envíe a través de nuestro SMTP (QMAIL). En este post se verá que hay que editar el php.ini para conseguir que PEAR funcione correctamente y permita al script enviar el email via SMTP.

Utilizaremos el código de ejemplo que está en Cibercity

# vim /root/scripts/mail.php

<?php
include("Mail.php");
/* mail setup recipients, subject etc */
$recipients = "feedback@yourdot.com";
$headers["From"] = "user@somewhere.com";
$headers["To"] = "feedback@yourdot.com";
$headers["Subject"] = "User feedback";
$mailmsg = "Hello, This is a test.";
/* SMTP server name, port, user/passwd */
$smtpinfo["host"] = "smtp.mycorp.com";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "smtpusername";
$smtpinfo["password"] = "smtpPassword";
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
/* Ok send mail */
$mail_object->send($recipients, $headers, $mailmsg);
?>

Lo ejecutamos como

# /usr/bin/php mail.php

PHP Warning:  include(Mail.php): failed to open stream: No such file or directory in /root/scripts/mail.php on line 2
PHP Warning:  include(Mail.php): failed to open stream: No such file or directory in /root/scripts/mail.php on line 2
PHP Warning:  include(): Failed opening 'Mail.php' for inclusion (include_path='.:') in /root/scripts/mail.php on line 2
PHP Fatal error:  Class 'Mail' not found in /root/scripts/mail.php on line 16

Cambiamos un poco el script así para añadir el path hasta la clase

include ("/usr/share/pear/Mail.php");
#include("Mail.php");

Lo ejecutamos otra vez

/usr/bin/php mail.php
PHP Deprecated:  Assigning the return value of new by reference is deprecated in /usr/share/psa-pear/Mail.php on line 134
PHP Deprecated:  Assigning the return value of new by reference is deprecated in /usr/share/psa-pear/PEAR.php on line 563
PHP Deprecated:  Assigning the return value of new by reference is deprecated in /usr/share/psa-pear/PEAR.php on line 566
PHP Warning:  include_once(Net/SMTP.php): failed to open stream: No such file or directory in /usr/share/psa-pear/Mail/smtp.php on line 206
PHP Warning:  include_once(): Failed opening 'Net/SMTP.php' for inclusion (include_path='.:') in /usr/share/psa-pear/Mail/smtp.php on line 206
PHP Fatal error:  Class 'Net_SMTP' not found in /usr/share/psa-pear/Mail/smtp.php on line 210

Se queja de la clase Net_SMTP pero parece que lo que pasa es que no lo encuentra. Volvemos a añadir el código include quedando así:

< ?php
include ("/usr/share/psa-pear/Mail.php");
include ("/usr/share/psa-pear/Net/SMTP.php");
#include("Mail.php");

Volvemos a ejecutar:

PHP Warning:  include_once(): Failed opening 'Net/SMTP.php' for inclusion (include_path='.:') in /usr/share/psa-pear/Mail/smtp.php on line 206
PHP Fatal error:  Class 'Net_SMTP' not found in /usr/share/psa-pear/Mail/smtp.php on line 210

Sigue quejándose aunque tenemos comprobado que la clase Mail o SMTP existe. Es un tema de configuración php.ini

Hay que seguir el tutorial de esta página para configurar bien el PEAR

http://pear.php.net/manual/es/installation.checking.php

En definitiva sería que editamos php.ini en la línea

;include_path = ".:"
include_path = "/usr/share/pear"

Reconfiguramos apache y volvemos a ejecutar. Si no da error es que era un problema de configuración PEAR PHP. Podemos ver si ha llegado con éxito a través del log de Qmail

Mar 25 11:38:14 s1 qmail: 1364207894.799250 delivery 33: success: 173.194.67.27_accepted_message./Remote_host_said:_250_2.0.0_OK_1364207897_wu7si4790118wjb.172_-_gsmtp/

Links
http://www.cyberciti.biz/tips/howto-php-send-email-via-smtp-authentication.html
http://goonanism.com/blog/2010/06/08/using-pear-to-send-email-via-smtp/
http://pear.php.net/manual/es/installation.checking.php

Leave a Reply

Your email address will not be published. Required fields are marked *