Rechercher dans ce blog

Chargement...

vendredi 13 novembre 2009

Zend_Mail without Zend framework ?

I have built a website in a classical manneer :
- Apache 2
- PHP 5
-MySQL 5
- powered by MAMP Pro

My app is NOT organized as a framework ! I just want to use Zend_Mail from the framework Zend, I just want to use the classes of Zend to send a mail. I have copied and pasted the good code to construct a message.
My issue : Zend_Mail don't reach the classes that are installed, so nothing works !!
These classes borry me for a while. I have used them a month ago in a Symfony project (that used Zend classes) and all worked fine !! It's because the framework Symfony, when you place the classes at the right place, reach easily (I don't know how does it do that, indeed!!) the classes the code needed !!

In non-framework website, you must organize yourself and write the good path between the classes and the files. It's  this point I've failed on !!

How is my app organized at the root ?
- differents folders with differents files in it (don't care for this case)
- "for_email" folder :
    - form.php
    - send_email.php
- "lib" folder :
    - "Zend" folder :
        - Exception.php
        - Loader.php
        - Mail.php
        - Mime.php
        - Loader folder
        - Mail folder
        - Mime folder
The folder "Zend" and the files/folders in it came from the Zend framework.

I wrote the code below in "send_email.php" file  and I obtained this  error message :

Warning: require_once(Zend/Mail/Transport/Abstract.php) [function.require-once]: failed to open stream: No such file or directory in /Users/flho/Sites/MARGE-UP/MUp-developpement/lib/Zend/Mail.php on line 26

Fatal error: require_once() [function.require]: Failed opening required 'Zend/Mail/Transport/Abstract.php' (include_path='.:/usr/local/php5/lib/php') in /Users/flho/Sites/MARGE-UP/MUp-developpement/lib/Zend/Mail.php on line 26



Voici le code :
require_once('../lib/Zend/Loader.php');
require_once('../lib/Zend/Mail.php');
 
$config = array('auth' => 'login',
                'username' => ' my username for smtp host',
                'password' => ' my password for smtp host';
 
$transport = new Zend_Mail_Transport_Smtp('my smtp host', $config);
 
$mail = new Zend_Mail();
$mail->setBodyHtml('my text in html mode');
$mail->setFrom('my email', 'my name');
$mail->addTo('the email to ', 'the name to');
$mail->setSubject('my email subject't);
$mail->send($transport);
 
La solution : problème de config PHP !!
1. The code written is good, EXCEPT the call of classes.

2. Zend_Mail is a part of the libraries of Zend, so Zend_Mail is a library AND MUST BE CALLED AS A LIBRARY , and not as a file !

For
project made with framework (Symfony for example), the libraries are
placed UPON the website root folder, so you can't reach them as a
normal file.
My project wasn't made with frameword, so I placed my
folder libraries INSIDE the root folder, BUT I MUST REACH THE CLASSES
THE SAME WAY !
The website is constructed like that :
Root folder :
   - some folders and somes files in it
   - "for_email" folder :
        - form.php
        - send_email.php
   - "library" folder :
           - "Zend" folder :
              - Exception.php
              - Loader.php
              - Mail.php
              - Registry.php
              - Validate.php
              - Mime.php
              - Loader folder
              - Mail folder
              - Mime folder
              - Validate folder

Nota : this folders came from Zend framework and haven't been modified.
 

So the code below is wrong :
// --------------------------------------------------
require_once('../lib/Zend/Loader.php');
require_once('../lib/Zend/Mail.php');
// --------------------------------------------------

and THIS is right :
//--------------------------------------------------
//-- reach the Zend Classes put in "library" folder :
$paths = array(
    realpath(dirname(__FILE__) . '/../library'),
    '.',
);
set_include_path(implode(PATH_SEPARATOR, $paths));
//-- for this script (only), you reach directly the Zend folder
require_once('Zend/Mail.php');
require_once('Zend/Mail/Transport/Smtp.php');
//--------------------------------------------------

This
code is written in "send_email.php" file. The rest of the code (begin
at line 4 in my first script : "$config=array(... and so on...) is
unchanged.

3. the last BUT NOT THE LEAST : the php.ini
You put the code in the right place and - Boom- "connection refused to smtp server" !!
It's possible you are hosted on shared hosting, so you can't modify the php.ini and , unfortunately, you have this directive :
allow_url_fopen = Off
That means you can't rely somme external url, like .... a smtp server !!!

Some
say "allow_url_fopen=On" is a lack of security, some say no : you just
need "register_globals=Off", it's sufficient. I don't wan't to discuss
of security lacks, because I'm not a master in it ! The facts are :

 - some shared hosters (eg :Ovh) have "allow_url_fopen=On" and you can
send emails via SMTP , using PHPMailer or Zend_Mail (I tested these
ones).
  - some shared hosters (eg: 1and1) have
"allow_url_fopen=Off" and You CAN'T SEND EMAIL VIA SMTP, using
PHPMailer or Zend_Mail !! Try to contact the support : they say you
just have to run with the PHP function : Mail. But for me, the Mail PHP
function don't work as good as SMTP (emails are sent slower and many
are judged as spam by pop client ...

so use Zend_Mail with allow_url_fopen !


0 commentaires: