• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Using PHP to send SMTP email using Zend

AndyB

Well-known member
#1
I would like to create a PHP script to send an email using SMTP.

The PHP script below is not working. Do I have to do anthing special to use Zend commands?

PHP:
<?php

$config = array(
'ssl' => 'tls',
'port' => 587,
'auth' => 'login',
'username' => 'example@gmail.com',
'password' => 'mypassword'
);

$smtpConnection = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);

$mail = new Zend_Mail();

$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('example@example.com', 'Some Sender');
$mail->addTo('example@example.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($smtpConnection);

?>
When I execute this script, I get the following error message in my Centos server log:

Code:
[Tue Feb 04 19:27:02 2014] [error] [client 25.5.96.60] PHP Warning: require_once(Zend/Mime.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in /path/to/www/forums/library/Zend/Mail/Transport/Smtp.php on line 27
[Tue Feb 04 19:27:02 2014] [error] [client 25.5.96.60] PHP Fatal error: require_once() [<a href='function.require'>function.require</a>]: Failed opening required 'Zend/Mime.php' (include_path='.:/usr/share/pear:/usr/share/php') in /path/to/www/forums/library/Zend/Mail/Transport/Smtp.php on line 27
 
Last edited:

Null

Well-known member
#2
I'm not sure as I don't use the Zend framework (well, I messed with it about 2 years ago).

PHP:
set_include_path('path/to/library/'.get_include_path());

require_once('Zend/Loader/Autoloader.php');

$autoloader = Zend_Loader_Autoloader::getInstance();

$config = array('ssl' => 'tls', 'port' => 25, 'auth' => 'login', 'username' => 'example@gmail.com', 'password' => 'password');

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

$mail = new Zend_Mail();

$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('sender@test.com', 'Sender Name');
$mail->addTo('recipient@test.com', 'Recipient Name');
$mail->setSubject('TestSubject');
$mail->send($transport);
It's late here (4:02am), I'll check in on the thread in the morning.
 
Last edited:

AndyB

Well-known member
#3
Okay I got it.

It appears that running the PHP code from a stand alone script is not the best way to test this.

I put the PHP code from post #1 into an add-on and it works great.
 
Last edited:

AndyB

Well-known member
#4
Thank you, Null. I really appreciate your time with this.

I will use the code in an add-on so I can use the code in post #1. I often like to test things in a stand alone script first to make sure they work.
 

Jeremy

Well-known member
#5
Why are you using the above code in XenForo? You should utilize the tools available to you in the software.
 

AndyB

Well-known member
#6
Why are you using the above code in XenForo? You should utilize the tools available to you in the software.
When I first started researching this, I didn't know what XF tools were available. Thankfully I got some help and I'm now using the proper XenForo code.

Here's what I ended up using in my add-on.

PHP:
// get visitor data
$visitor = XenForo_Visitor::getInstance();

// username
$username = $visitor['username'];

// email 
$email = $visitor['email'];

// emailSubject
$emailSubject = new XenForo_Phrase('newthreademail_subject');

// emailMessage
$emailMessage = new XenForo_Phrase('newthreademail_message');

// define user variable
$user = array(
    'username' => $username,
    'email' => $email
);

// prepare mailParams					
$mailParams = array(
    'user' => $user,
    'subject' => $emailSubject,
    'message' => $emailMessage
);
    
// prepare mail variable
$mail = XenForo_Mail::create('contact', $mailParams);

// send mail
$mail->send($user['email'], $user['username']);
 

TBDragon

Active member
#7
When I first started researching this, I didn't know what XF tools were available. Thankfully I got some help and I'm now using the proper XenForo code.

Here's what I ended up using in my add-on.

PHP:
// get visitor data
$visitor = XenForo_Visitor::getInstance();

// username
$username = $visitor['username'];

// email
$email = $visitor['email'];

// emailSubject
$emailSubject = new XenForo_Phrase('newthreademail_subject');

// emailMessage
$emailMessage = new XenForo_Phrase('newthreademail_message');

// define user variable
$user = array(
    'username' => $username,
    'email' => $email
);

// prepare mailParams                   
$mailParams = array(
    'user' => $user,
    'subject' => $emailSubject,
    'message' => $emailMessage
);
   
// prepare mail variable
$mail = XenForo_Mail::create('contact', $mailParams);

// send mail
$mail->send($user['email'], $user['username']);
can i use this in my forum as i work on something slimier to this .

good luck in ur addon ^_^
 

TBDragon

Active member
#9
Of course you can. If you look through XenForo files it's pretty much how XenForo does it.
i didnt know where to search and i was busy thats y i make it from scratch some how using the Mailer script and SMTP and so many things >_< will make my life hard but with this way i can do it more easier .