Guasqueño
Guasqueño

Reputation: 447

Cannot send mail using Zend library

require_once does not seem to work. I am trying to use Zend libraries to send mail but something does not work. I am using my web hosting server provider so I have to put the libraries in a subdirectory (without installing any framework).

// $path= dirname(__FILE__)."/ZendLibrary:".get_include_path() ;
$path= "./ZendLibrary".PATH_SEPARATOR.get_include_path() ;
set_include_path($path);
//echo $path ;

echo "Spot 0" ;
require_once 'Zend/Mail.php';
echo "Spot 1" ;

I got the "Spot 0" message but I don't get the "Spot 1" message. I have selected only two Zend libraries:

 ZendLibrary/Zend/Mail/* (directory)
 ZendLibrary/Zend/Mime/* (directory)
 ZendLibrary/Zend/Mail.php (script)
 ZendLibrary/Zend/Mime.php (script)

ZendLibrary is a folder in the same directory where my script is. What could happening?

UPDATE#1:

My problem is that the code stops working right when I run require_once. It does not echo "Spot 1" message. I have tried absolute paths for set_include_path, I have tried to load a .php library script without internal require_once statements, but nothing makes it work. As my test is run from a web hosting site in the Internet I don't have access to logs!.. or do I?

$path= realpath(dirname(__FILE__).'/ZendLibrary').PATH_SEPARATOR.get_include_path() ;
// $path= "./ZendLibrary".PATH_SEPARATOR.get_include_path() ;
set_include_path($path);

Upvotes: 1

Views: 1700

Answers (4)

Guasqueño
Guasqueño

Reputation: 447

Answer:

My ISP (web hosting too) has a setting to disable require_once. I was able to setup a Unbuntu virtual machine on VirtualBox and set up a web server. I copied the content of my test web site in my Unbuntu and find out that require_once does work as expected !!! That may explain why the server-side include did not work for me either. The following was a usefull check, that indicated me the file was in the expected place.

$file = 'ZendLibrary/Zend/Mail.php'; 

if ((!is_file($file)) or (!is_readable($file))) 
   die("File does not exists or is not readable."); 

Now I have to find out why it is disabled, how to enable it, or just switch web hosting company. Update: I had to pay $20 dolars a year more for the provider to enable PHP (in this case to be able to include).

This site can tell you more about it.

Upvotes: 0

Elzo Valugi
Elzo Valugi

Reputation: 27856

Zend Mail has some dependencies. Hard dependencies

  1. Zend_Exception
  2. Zend_Loader
  3. Zend_Mime
  4. Zend_Validate

Soft

  • Zend_Locale
  • Zend_Date
  • Zend_Filter
  • Zend_Registry

The best is to add all Zend library to the include path in PHP and all components will load the dependencies automatically.

Upvotes: 1

drew010
drew010

Reputation: 69927

I used Zend_Mail with SMTP standalone before, here are the files I needed. I also reduced it down to what you need if you only want to use sendmail also.

If you want to use Sendmail, that is the easiest. Your dependencies are:

  • Zend/Exception.php
  • Zend/Mail.php
  • Zend/Mime.php
  • Zend/Mail/Exception.php
  • Zend/Mail/Transport/Abstract.php
  • Zend/Mail/Transport/Exception.php
  • Zend/Mail/Transport/Sendmail.php
  • Zend/Mime/Exception.php
  • Zend/Mime/Message.php
  • Zend/Mime/Part.php

And with those files, here is an example use:

<?php
// optionally
// set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/Zend');

require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Transport/Sendmail.php';

$transport = new Zend_Mail_Transport_Sendmail();

$mail = new Zend_Mail();
$mail->addTo('user@domain')
     ->setSubject('Mail Test')
     ->setBodyText("Hello,\nThis is a Zend Mail message...\n")
     ->setFrom('sender@domain');

try {
    $mail->send($transport);
    echo "Message sent!<br />\n";
} catch (Exception $ex) {
    echo "Failed to send mail! " . $ex->getMessage() . "<br />\n";
}

If you need SMTP, then you have a few more dependencies as prodigitalson showed. In addition to the above you need at least:

  • Zend/Loader.php
  • Zend/Registry.php
  • Zend/Validate.php
  • Zend/Mail/Protocol/Abstract.php
  • Zend/Mail/Protocol/Smtp.php
  • Zend/Mail/Transport/Smtp.php
  • Zend/Validate/Abstract.php
  • Zend/Validate/Hostname.php
  • Zend/Validate/Interface.php
  • Zend/Validate/Ip.php
  • Zend/Validate/Hostname/*
  • Zend/Mail/Protocol/Smtp/Auth/*

Then you can do something like this:

<?php

require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Transport/Smtp.php';

$config    = array(//'ssl' => 'tls',
                   'port' => '25', //465',
                   'auth' => 'login',
                   'username' => 'user',
                   'password' => 'password');

$transport = new Zend_Mail_Transport_Smtp('smtp.example.com', $config);

$mail = new Zend_Mail();
$mail->addTo('user@domain')
     ->setSubject('Mail Test')
     ->setBodyText("Hello,\nThis is a Zend Mail message...\n")
     ->setFrom('sender@domain');

try {
    $mail->send($transport);
    echo "Message sent!<br />\n";
} catch (Exception $ex) {
    echo "Failed to send mail! " . $ex->getMessage() . "<br />\n";
}

Upvotes: 0

prodigitalson
prodigitalson

Reputation: 60413

You havent met all the dependencies... At the minimum you also need Zend_Exception (all component specific exceptions in the framework extend form this) but im pretty sure there are others that Mail and Mime depend on. Just to make it easy on myself i would also grab Zend/Loader and use it for autoloading.

Update:

I took a look and it seems as though you will also need Zend/Validate and Zend/Loader which is required by one of the classes in Validate component - though Mail only uses Zend/Validate/Hostname (which does depend on the abstract classes and interfaces for Validate as well as Zend_Validate_Ip) so you might be able to get away with not grabbing Zend/Loader, but you might as well jsut make use of it for general purpose autoloading.

As a suggestion you could use [Swift Mailer]1 instead... youll be bale to avoid dependency hell and i like it a lot better anyhow. Only down side is its for Sending mail, while Zend_Mail will also let you read a mail store on a local or remote server.

Upvotes: 0

Related Questions