Jump to content
Larry Ullman's Book Forums

Zend mail not sending emails

Recommended Posts

Hi Larry,


Happy New Year!


I'm having problems sending and receiving emails via the zend-mail procedure.

The code on page 434 runs fine without any errors, but I am just not receiving any emails.

I used the exact code on page 434, but that wasn't working, so I tried to configure smtp settings, thinking that may be the missing solution, which is:


// Create a new mail:
use Zend\Mail;
use Zend\Mime\Message as MimeMessage;
use Zend\Mime\Part as MimePart;
use Zend\Mail\Transport\Smtp as SmtpTransport;
use Zend\Mail\Transport\SmtpOptions;

// Create the parts:
$html = new MimePart($body_html);
$html->type = "text/html";

$plain = new MimePart($body_plain);
$plain->type = "text/plain";

// Create the message:
$body = new MimeMessage();
$body->setParts(array($plain, $html));
// Establish the email parameters:
$mail = new Mail\Message();
$mail->setFrom('myemaill@aol.com', 'Dmx');
$mail->addTo($_SESSION['email'], 'Tom');
$mail->setSubject("Order #{$_SESSION['order_id']} at the Coffee Site");

// Setup SMTP transport using LOGIN authentication
$transport = new SmtpTransport();
$options   = new SmtpOptions(array(
    'name'              => 'localhost',
    'host'              => 'localhost',
    'connection_class'  => 'login',
    'connection_config' => array(
        'username' => 'myusername',
        'password' => 'mypassword',



The above code then gave me the following error:


'An error occurred in script '/Applications/MAMP/htdocs/vendor/zendframework/zend-mail/src/Protocol/AbstractProtocol.php' on line 223:
stream_socket_client(): unable to connect to tcp://localhost:25 (Connection refused)'


So I'm at a loss as to what to do now.

I know the zend-mail code has changed a lot since you wrote the book, but I just can't seem to find any success in finding the correct code needed to send and receive emails.


Qu: Please could you help, by giving me the correct code needed to send emails using zend-email.

I'm using a mac and mamp.


Thank you




Link to comment
Share on other sites

For what it's worth, I almost never bother with trying to send email from a local environment. It's just a huge PITA and there can be a ton of reasons why it's not working. It doesn't look like MAMP comes with a mail server, so you'll need to use SMTP to either your ISP's email server or a third-party (e.g., Gmail). Your ISP may or may not allow this, though. But if you google "<your ISP> smtp settings" you might be able to find the settings you need. 

Link to comment
Share on other sites

Ok, that's fine.

So would I be able to use the zend mail in a production environment? Meaning, if I upload the whole application to my hosting provider, will i be able to use their smtp/email settings along with the zend mail code you wrote, to implement sending emails using zend mail?

Link to comment
Share on other sites

Qu 1: What I mean, is zend mail secure and efficient enough to be used as a professional email sender in a fully functional ecommerce website?

Qu 2: if zend mail is not meant for a professional environment or not secure enough, what professional email sender would you recommend?





Link to comment
Share on other sites

Okay, in looking at ZF, as of a year ago it's now been converted to an open source project: https://framework.zend.com/blog/2020-01-24-laminas-launch So in a production environment you'd use Laminas mail, not ZF (https://docs.laminas.dev/laminas-mail/). It should be secure and efficient enough and would work in a hosted environment.

Another alternative is to use a third-party email service like Mailgun or Sendmail. Both cost money but provide additional features, such as detailed logs, protection from spam (i.e., your mail server being used to send spam), greatly improved success in an email being accepted, etc. Mailgun is only like $20/month for 1k emails, if I recall correctly. These make sense if you're willing to spend a bit more money up front and don't want to run your own mail server at all. 

Link to comment
Share on other sites


  • Create New...