Transfer all new e-mails to a database - php

I am using postfix for my linux mail server. The goal is to have any incoming mail dumped into a database with the headers and message information, then the e-mail being deleted from the mail server. Is there any way to make postfix post a message to a php file everytime a new e-mail comes in then delete the e-mail message? The only other way I can see to make a script to poll the e-mail server, read each mail and transfer the contents to a database, then delete the messages from the mail server. Being able to have postfix automatically execute the php script for all new incoming mails would be a better choice. If it makes a difference, the mail server and the server with the php file is the same. Any direction in this matter would be greatly appreciated.

use .forward, /etc/aliases, hashtable etc to forward mail to a script.
In /etc/aliases, I have
mysite-confirm: |/home/mysite/confirm.sh
In confirm.sh, I have
#!/bin/sh
basedir=/home/mysite/www
php -d include_path=$basedir/includes -f $basedir/cli/confirm.php
In confirm.php, the magic happens:
$contents = file_get_contents("php://stdin");
do_magic_with_mail($contents);
All quite simple and rigid. The only downside is that you could mail mysite-confirm#any_domain_I_host.com, but you can fix that with the right aliases / virtualmaps etc.

Related

How to get PHP to send mail to local users (cakePHP, Linux SLES)

Currently I have a mail server configured (a real one from my ISP) and mail internal and external works on the command line. In PHP only external users work.
For testing I would like to send to internal users only.
( Ideally I would like to set up lots of aliases that point to one user so mail to:
tom#localhost.com, dick#localhost.com, harry#localhost.com end up in /var/mail/johnsmith )
I'd be greatful if someone could help here. I'm hesitant to edit the postfix config files...
On the command line johnsmith#localhost works but not in PHP. It's using cakePHP and I checked the value of $email-addr just before the send ($this->Email->send();) and the value is johnsmith#localhost. I'm not that familiar with cakePHP yet. The var/log/mail shows nothing, only external email addresses.
(server is Suse linux)
You can use the basic mail php function
http://php.net/manual/en/function.mail.php
Under linux, mail php function relies on sendmail, just check that sendmail is properly installed.
In /etc/postfix/main.cf add localhost.com:
mydestination = $myhostname, localhost.$mydomain,localhost,localhost.com
This allows sending on "localuser#localhost.com" via command line. I loaded up a test php mail form script in a browser and it works, sending mail through to /var/mail/localuser.
At the moment it means I have to check each local users /var/mail file. I'm working on the alias. My first attempt at that failed.

How to display or save emails instead of actual sending them on localhost?

I'm working on a php project where all emails are sent via the mail() function.
That's quite a problem to prepare and test those emails because mail() function fails on the localhost and I should constantly rewrite my code to print out the email before sending, check it and asume that on server it would be sent ok.
Is there a way to somehow manage such situations?
I will be very happy if there is a way to save the messages on hard disk or send them only to the one specific email address, not to the real recipients, without or with slight modification of the code. Some useful software or advices are so appreciated.
Thanks!
P.S. On the localhost I'm using WAMP package as a webserver.
http://www.toolheap.com/test-mail-server-tool/
Test mail server tool for Windows is awesome! Every time you send an email on local host, it just pops that email up in your favorite email reader (I.e. outlook, postbox etc)
I use it exclusively to test all my web apps on WAMP!
No server changes needed - just download and install - then send an email and see it in action.
Oh - and it's free!
Here is the solution I found to get around this problem.
1. Create the sendmail.php file somewhere in the wamp dir, e.g. d:\wamp\apps\sendmail.php. Here is it's source:
/* Path where emails will be stored */
define('DST', 'd:/wamp/tmp/sendmail/');
/* Extract the message from the stdin */
$message = '';
while(($line = fgets(STDIN)) !== false) {
$message .= $line;
}
/* Save message to file */
file_put_contents(DST.date('Y-m-d_H-i-s_').md5($message).'.eml', $message);
2. Uncomment and edit the sendmail_path parameter in the php.ini to this:
sendmail_path = "D:\wamp\bin\php\php5.3.5\php.exe D:\wamp\apps\sendmail.php"
All messages that are sent via the mail() function will be captured and stored in the specified directory.
Install fakemail. It acts as a local SMTP server and saves all mails in a folder.
You could wrap the mail() function, like:
function my_send_mail(/*...*/) {
if (is_localhost()) {
// just save the mail to text.
} else {
// call mail() and send mail
}
}
And use this function instead of use mail() directly.

php redirect email to file

I am using php5.
Are there some settings or a simple php.ini directive that would redirect all the emails to a folder?
I want on the development machine to have all the emails generated by the system not sent to the actual receiver but put in a folder.
Thanks.
I used to have some code like this (kinda pseudocode):
define ('DEBUG', true);
function send_email($to, $subject, $body) {
if (DEBUG) {
file_put_contents('some_folder/' . $to . date('dmY-His') . '.html', $body);
}else{
// Actual code to send email
}
}
But i agree with others, it's easier/better to setup an development email account to receive those emails.
I don't think you will be able to do something like this. Mails are sent by a mail server so it must be your mail server that writes them to a file instead of sending them.
Why not simply send it to a special development email?
Sample:
define('DEBUG', true);
if(DEBUG)
{
// Override recipient
$recipient = 'development#domain.tld';
}
// Send mail...
No settings that I'm aware of in PHP itself. However, if you're using Postfix on your development server, here's a recipe I cooked up to redirect all outbound email to a single (local) address:
/etc/postfix/main.cf: (add this to the existing file, don't replace everything)
virtual_alias_maps = regexp:/etc/postfix/virtual
/etc/postfix/virtual:
/.*/ duskwuff#localhost
You can configure your mail server to accept SMTP messages as normal, but make it unable to forward them onto another mail server. If your mail server supports it, make it redirect all messages to a postmaster account, or any other address of your choice.
This means that PHP will behave as normal, everything will appear to work straight away with the message, but it just won't go to the 'intended' recipient.
It also means that you can inspect headers (pretty much as they would normally appear), to support debugging.
There are many ways to do this. Basically, you need to define the sendmail command in your php.ini to point to a program or script which will save the mail locally.
One solution is this:
Catch emails with php script
Another is this:
Mail catcher

PHP = > if (mail) + timeout?

I have a simplistic webform running on bluehost that when submitted, sends out mail to a distribution list.
Recently the mail server went down, and my php mail script hung when the submit button was clicked. The user tried one more time, and again the page just hung. Later when the mail server came back up, it sent out multiple copies of the mail.
My question is, is there an easy way to put some sort of timeout on the script, so that if either a set period of time passes and the mail server has not acknowledged the request, or alternatively, if the number of unsuccessful attempts has exceeded a preset number, then the script will stop attempting to send the mail?
My script:
// try to send email
if (mail($to,$subject,$msg,$headers)) {
header('Location: complete.php');
} else
{
header('Location: incomplete.php');
}
Thanks for any advice you may be able to give,
Rich.
PS. I do not have access to change any settings on the server, although I do have a .htaccess file saved to the local directory.
This is not possible. You may want to have a look at phpmailer which is way more advanced than the built-in mail() function php provides.
mail() always returns true if the mail has been submitted to the mailserver (and actually should not hang when the mailserver is down, but return false).

How to write a PHP script that read bounce email?

I am doing a bounce-email handling with PHP. I have include the return path in the mail function, e.g:
mail($to_address, $subject, $message, $headers, "-f".$return_path );
$return_path = "bounce_handle#domain.com";
Now, what should my php script looks like (and where should i put it) in order to read all the bounce emails? (can show me with some sample code?)
You'll need to configure whichever mail transport agent handles (MTA) "bounce_handle#domain.com" to send the mail to the PHP script that does whatever magic you need it to do. The MTA is what actually handles mail coming into the server. There are many different MTA's, but most of them have some configuration where you can basically tell it to pipe email coming into a certain address into a custom script.
Alternatively, you could setup a mailbox for your bounce handler and have PHP read it via POP3. For this, you'd have to configure an actual email account for your bounce handler. Then you have your PHP script connect to that mailbox using standard protocols. See the php.net documentation on IMAP/POP for how this is accomplished.

Categories