Zend Framework is currently one of the best MVC-based frameworks in the PHP world. Zend_Mail is part of Zend Framework and it provides the ability to easily send email messages. If you’re like me, most web applications you have developed are setup to use Google Apps as their email provider. Here’s how to send email messages via Gmail or Google Apps by using Zend_Mail.
<?php
public function send() {
//Initialize needed variables
$your_name = 'Mario Awad';
$your_email = 'your_email@your_domain.com'; //Or your_email@gmail.com for Gmail
$your_password = 'your_password';
$send_to_name = 'My Friend';
$send_to_email = 'myfriend@example.com';
//SMTP server configuration
$smtpHost = 'smtp.gmail.com';
$smtpConf = array(
'auth' => 'login',
'ssl' => 'ssl',
'port' => '465',
'username' => $your_email,
'password' => $your_password
);
$transport = new Zend_Mail_Transport_Smtp($smtpHost, $smtpConf);
//Create email
$mail = new Zend_Mail();
$mail->setFrom($your_email, $your_name);
$mail->addTo($send_to_email, $send_to_name);
$mail->setSubject('Hello World');
$mail->setBodyText('This is the body text of the email.');
//Send
$sent = true;
try {
$mail->send($transport);
}
catch (Exception $e) {
$sent = false;
}
//Return boolean indicating success or failure
return $sent;
}
In addition to the above code, please note the following:
I hope this small tutorial saves you some headaches.
Cheers!
Founder of SOFTKUBE, lead developer, and getting things done addict. Passionate about open source, user interface design, business development, and the tech world.
A small team of experts developing simple, usable, and high-quality web solutions. We blog about business, entrepreneurship, web development, and technology.
Gaining Access to a Legacy Google Apps Account When Phone Verification Fails
Custom Theme Migration from Drupal 9 to Drupal 10
How to Change the Most Recent Git Commit Message
How to Make Google Chrome Forget a Permanent HTTP 301 Redirect
Business Cheat Sheets CLI Design Development Downloads Drupal Email Google Apps HID Keyboards Multilingualism Open Source Philosophy PHP Pointing Devices Productivity Quotes Science Security SEO Technology Thoughts Windows Zend Framework