Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Not Available

Guest
Original poster
Jun 30, 2009
318
0
Well... I'm desperate about this. I've been trying to send an email using the PEAR::Mail() function, and nothing seems to work. I've now got this code, which doesn't work at all. What am I doing wrong?

Code:
if(!empty($to) && !empty($subject) && !empty($text)) {
require_once('/Applications/MAMP/htdocs/PEAR/Mail.php');
$smtp = array("host"=>"ssl://smtp.gmail.com", "port"=>"465", "auth"=>true, "username"=>"my_mail@gmail.com", "password"=>"my_pass");
$mail = Mail::factory("smtp", $smtp);

$headers = array("From"=>$from, "Subject"=>$subject, "Cc"=>$cc, "Bcc"=>$bcc);
$sent = $mail->send($to, $headers, $text);
    	
     if(PEAR::isError($sent)) {
    	echo "<p>" . $sent->getMessage() . "</p>";
     }
}
 
Why do you have PEAR installed in your htdocs folder? PEAR comes pre-installed with MAMP in the /Applications/MAMP/bin/php5/lib/php/PEAR/ folder.

To install additional packages simply type the following in a terminal window;

Code:
/Applications/MAMP/bin/php5/bin/pear install -a Mail-1.1.14

Note: Mail-1.1.14 may be a little out-of-date now. You should check on the PEAR website first.

The -a flag instructs the installer to include all dependencies. For the PEAR Mail package these are Net_Socket, Auth_SASL and Net_SMTP. After installation these can be found in sub-folders of the /Applications/MAMP/bin/php5/lib/php/ folder.

Here's some PHP code to test the install. Works like a charm for me.

PHP:
<?php
require_once "Mail.php";

$from = "Steve <a@b.com>";
$to = "Steve <a@b.com>";
$subject = "[Site Message]";
$body = "Hello";

$host = "outgoing.mail.server";
$username = "username";
$password = "password";
$port = "80";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
  	'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");
 }
?>
 
One more question: I went to /Mail/smtp.php, and edited the info there. How do I use it now? :D Do I also include smtp.php, or is Mail.php enough? And do I create the array for the second parameter, or is it already created in some variable?
 
Not sure why you need to edit smtp.php. The PHP code I since included in my first post should work, after installation of the Mail package and dependencies, without any tweaks. Mail.php alone should suffice. Take a look at the code.
 
I haven't edited Mail.php. It's just that the Mail package also includes an smtp.php file in the Mail folder, which contains all the data needed (host, username, pass, auth, port). So I edited that one, and I don't know how to give Factory::Mail the second parameter based on the info I entered in there.
 
I haven't edited Mail.php.

Ooops. My bad. Changed to smtp.php

Again, you shouldn't need to edit any of the installed files. The values host, port etc. are declared in your .php code.
 
Pear Mail

I am lost.
I have downloaded Pear's mail 1.2 but what can I do with it?
I do not have any such directory as "/Applications/MAMP/"
The applications folder does not have a MAMP folder and I would not expect that it would have. That is such an obscure name something tells me it would be hidden away in some invisible folder... not the applications folder. Yet I still checked... to no avail.
I checked in the Library, system, user folders... everywhere I could.

I have a dual quad core mac pro, all the latest system updates etc, and would love to have this Pear mail working.

Please advice.
:apple:
 
I am lost.
I have downloaded Pear's mail 1.2 but what can I do with it?
I do not have any such directory as "/Applications/MAMP/"
The applications folder does not have a MAMP folder and I would not expect that it would have. That is such an obscure name something tells me it would be hidden away in some invisible folder... not the applications folder. Yet I still checked... to no avail.
I checked in the Library, system, user folders... everywhere I could.

I have a dual quad core mac pro, all the latest system updates etc, and would love to have this Pear mail working.

Please advice.
:apple:

I don't know anything about PEAR, but MAMP is an application you have to install that lets you run PHP on your local machine.
 
I don't know anything about PEAR, but MAMP is an application you have to install that lets you run PHP on your local machine.

Thanks. Got MAMP.
I copied it to the applications folder as directed. Opened the app, nothing apparently useful inside the app....

Tested the php script above with my SMTP and password etc...

I get this error:
"Warning: include_once(Net/SMTP.php): failed to open stream: No such file or directory in /Library/WebServer/mydomain/Mail/smtp.php on line 348 Warning: include_once(): Failed opening 'Net/SMTP.php' for inclusion (include_path='.:/usr/lib/php') in /Library/WebServer/mydomain/Mail/smtp.php on line 348 Fatal error: Class 'Net_SMTP' not found in /Library/WebServer/mydomain/Mail/smtp.php on line 349"

I do indeed have a Mail folder with a smtp.php file, and the access permissions are read + write.
I don't get it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.