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.
Related
I am piping all e-mails through a PHP script that checks the To address against a database of valid addresses. If it exists, the rest of the script handles it. However, if it does not exist, how can I bounce the e-mail, the same way the server would if I didn't have the script? Thanks!
The mails are recieved through smtp protocol, in your case also i assume that there is some service running on port 25 which would listen to request for mail from external domains.
There are different ways to bounce the message
->bounce at smtp level itself, as in when u get the recipient list, check f
or the id existance and if does-not exists give a 4xx response. The bouncemai
l would then be generated by the senders domain automatically.
-> if you have accepted the mail from the domain say gmail.com then u will
have to make a new connection to gmail with your bouncemessage, this is same
as sending a new mail from your server to gmail.com.
Turns out this was very easy to solve: Simply echo something in the PHP script (for example, "This account does not exist.") and the mailer daemon generates a bounce-back e-mail with this output included.
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.
when sending newsletters through a SMTP server using PEAR's Mail package, is there any way to specify some kind of "connection reuse" so that the PHP script won't have to create a new socket to the SMTP server for each individual mail?
That is of course without putting the adress of each recipient in only one e-mail, so that the indvidual recipient's won't see each others adresses.
Or doesn't SMTP allow for this?
Well I think the best solution is to put each destinee in black carbon. This guarantees that who receives the mail does not see other mail addresses and is a better solution than sending a mail for each destinee
This is feasible with php pear Mail package.
PEAR Mail seems to be a hopeless case, but Zend's framework has addressed the issue and keeps the SMTP socket open for as long as the script runs (and the object exists): http://framework.zend.com/manual/en/zend.mail.multiple-emails.html
What you should do is set the 'persist' param. And then only use the factory method once - then you ensure that it is the same socket that is used.
something like this:
static $mail;
if (!is_object($mail)) {
$mail = Mail::factory($options['mail_method'], $params);
}
$res = $mail->send($to, $mime_headers, $body);
If you call the mail::factory every time then a new socket will be created. In the above way you only create one socket.
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
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.