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.
Related
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
After sending an e-mail using PHPMailer to my Gmail account, after clicking 'show details,' to the right of 'mailed-by,' it says 'yourhostingaccount.com.' Here is a picture:
I've read you may change it using the fifth parameter of PHP mail(), although I'm not using that. Is there a way to change this using PHPMailer? Thanks!
Have you tried:
$mail->Host = "domain.com";
I found out that my domain and hosting provider, FatCow, uses NS1.YOURHOSTINGACCOUNT.COM and NS2.YOURHOSTINGACCOUNT.COM as their domain servers. This explains why it is happening, as I was connecting via SMTP to an e-mail address hosted by them. Unfortunately, I don't think I can change that and solve my problem... oh well.
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
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.
I'm developing a zend framework application that includes a simple email function. The development version is running on my computer, which is running Ubuntu. The production version is going to run on a production server.
When trying to send a test email to myself, I get an exception with the message: "Unable to send mail". I don't know if this is an environment issue, or a code issue. I'm not using a transport so I think it is defaulting to Zend_Mail_Transport_Sendmail. Here's my code:
public function sendtestAction()
{
$mail = new Zend_Mail();
$mail->setFrom('test#aol.com', 'Test Email');
$mail->addTo('my#email.com', 'My Name');
$mail->setSubject('This is just a test.');
$mail->setBodyText('This is only a test.');
$mail->send();
}
Update: I tried a different approach by setting the SMTP transport to use localhost:
transport = new Zend_Mail_Transport_Smtp('localhost');
Zend_Mail::setDefaultTransport($transport);
I got a different error this time: "Connection refused" Not sure what that means. Maybe I haven't set something up yet?
Update: I guess I didn't have an SMTP server installed/setup. This tutorial made it really easy for me to get an SMTP server up an running. Now both of the code samples above work.
It sounds like you need to configure an MTA, or find one that you can send to. Ubuntu desktop should set one up by default, probably either exim or postfix, but if you haven't configured it, it will unlikely to be running.
You don't want to set the default transport if you wish to use sendmail (it is the default) and SMTP is different.
That it doesn't send the emails suggests that sendmail or the MTA on your server is not installed/not setup correctly.