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
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 = me@example.com
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.
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.
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 = "you@yourmailserver.net";
$password = "password";
$port = 25;
$headers = array ('From' => "Your agent <noreply@yoursite.net>",
'To' => $recipient,
'Subject' => $subject
);
$smtp = Mail::factory(
'smtp',
array ('host' => $host,
'auth' => true,
'port' => $port,
'username' => $username,
'password' => $password)
);
$smtp->send($recipient, $headers, $body);
}
?>
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.
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.
I think the best solution is write all messages to file. So you just need to make own sendmail.
add to httpd.conf file this strings:
php_admin_value sendmail_path
"/Applications/MAMP/somefolder/mysendmail.sh"
In the file mysendmail.sh add following:
#!/bin/bash
while read line
do
echo "$line" >> ../mail_log.txt
done
echo "------------- next mail ----------------" >> ../mail_log.txt
exit 0
Do not forget to set privilegies: chmod 755 mysendmail.sh