views:

1403

answers:

5
+3  Q: 

php mail on MAMP

hi, I need to test some script using PHP's mail. I'd like to be able to finally get this working locally. I am using MAMP. Is there a way to do this without installing any third party software?

I've done some searching on this but haven't found anything appealing.

Thanks

A: 

Few months ago I had a similar problem whilst developing on my local machine an application which involved sending automating email notifications. I have lost quite some time installing Sendmail on OSX and eventually I could not get it working right..

My approach was to use the PEAR Mail as a temporary replacement for php's native mail function. Basically you can define a function called send-mail (see code below) and, once you deploy your app on a server, you can possibly replace the calls to that function with calls to mail().

     <?php
     require_once 'Mail.php';
     function send_mail($recipient,$subject,$body){

            $host = "yourmailserver.net"; 
            $username = "[email protected]";
            $password = "password";
            $port = 25; 

            $headers = array ('From' => "Your agent <[email protected]>",
              'To' => $recipient,
              'Subject' => $subject
            );  

            $smtp = Mail::factory(
             'smtp',
              array ('host' => $host,
                'auth' => true,
                'port' => $port,
                'username' => $username,
                'password' => $password)
            );  
            $smtp->send($recipient, $headers, $body);
       }
    ?>
And
A: 

what i do is i use the phpmailer class (warning: horrible website !) and specify a real smtp server on which i have an account. So i don't use mail() but use smtp. In this way, it does not matter whether i'm on my local server or on the real server. But you do need a working smtp access to that smtp mail server. Best would be to actually use the production mail server (the one that will be used by your application when it goes live). In this manner, you won't have last minute surprises when you discover that the mailserver messes up the reply-to field and little things like that.

pixeline
The OP has stated that he's using MAMP where PHP has no conept of a local MTA and mail is always sent via SMTP to the configured server. phpmailer does add a lot of other functionality conmpared with PHP's mail, but nothing relevant to the original question.
symcbean
it does make using smtp super easy, hence my suggestion.
pixeline
A: 

You might want to consider the Swift Mailer library

http://swiftmailer.org/

It makes doing email from PHP code much more reliable. You could even point your mailer script to a real SMTP service. This can eliminate a a lot of issues you would run into when moving from local to to production environments.

Using swift mailer is as simple as using a single include at the top of your PHP script and writing a code block to send a simple message. And it is fully object oriented.

Gordon Potter
+1  A: 
symcbean
A: 

You could use your gmail account and send your test emails via gmail's SMTP server.

You can use the phpmailer class (http://phpmailer.worxware.com/) to do this. There is a basic gmail example in the examples/ folder when you download this class.

Julien