php mail on MAMP

26,714

Solution 1

Are you specifically trying to test the sending of mail, or are you testing the rest of the code?

In the case of the former, you need to configure:

 SMTP = smtp.example.com
 smtp_port = 25
 sendmail_from = [email protected]

in your php.ini file (check where it is with phpinfo()), substituting in appropriate values.

To test the code other than the process of sending mail, then I'd recommend creating 2 include files:

<?php
// for live usage/mail send testing
function ori_mail()
{
   return call_user_func_array('mail',func_get_args());
}

and for testing other code

function ori_mail()
{
   file_put_contents('debug_mail_scripts.txt'
       ,date('r') . ':' . var_export(func_get_args(), true)
       , FILE_APPEND);
}

And include the relevant one to your testing.

Note that testing integration with the SMTP server, and testing deliverbility of your code is rather complex but should be done independently of testing your PHP.

C.

Solution 2

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.

Solution 3

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);
       }
    ?>    

Solution 4

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.

Share:
26,714
Ori
Author by

Ori

going to school for cpsc and math.

Updated on July 14, 2022

Comments

  • Ori
    Ori almost 2 years

    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