TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-23-2011, 02:03 PM   #1 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default PHP mail function

I currently have a live website running the php mail function which results in up to 50 emails being sent out when one form is submitted on the website. The code drawns if from a mysql database. It then loops through this array of email address and calls the php mail function to send out each email. Problem is on my live shared hosting environment it runs like a dog and the browser hangs for up to 10 seconds while this process runs. What is the solution. Without any major changes would it be better if i called the mail function once and passed in the array. Can i do this? Any help guys? CHeers.
captainmerton is offline  
Reply With Quote
Old 10-29-2011, 02:59 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

What you could do is use the BCC option, and attach everybody's email to that. Then you can send out one email, and the email addresses wouldn't be exposed. However, with that said, if you're wanting to write a personal email to each individual user, such as changing their name, then unfortunately that's not possible with the BCC option. It would send the same email to everybody.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-05-2011, 08:43 AM   #3 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

If i bcc the recipients wont they still be able to view the other recipients in the email header protocols?

Looking to try and understand from a design perspective what is the best way to do this i.e. someone submits an enquiry form and it loops through all property rental owners and emails them currently around 55 owners. I can see this process starting to take longer and longer in the browser. Its like the php mail function is generating lots of overhead. I am guessing i could just remove it then use a CRON job to send the emails every 15mins or something. Thoughts?
captainmerton is offline  
Reply With Quote
Old 11-07-2011, 11:14 PM   #4 (permalink)
HersheysBlog.com
 
Hershey's Avatar
 
Join Date: Nov 2011
Posts: 15
Thanks: 0
Hershey is on a distinguished road
Default

As soon as I read that your browser locks up, thr first thought that came to my mind is that you could create a page that when you hit submit, it takes you to a page that has some kind of message that says "Sending.. please wait." or something, then at page will actually call the action page that processed what you needed it to do. This won't fix the lock up but it will tell the users that it is working. Or you could have an ajax page pop up saying it's working.

Quote:
I am guessing i could just remove it then use a CRON job to send the emails every 15mins or something. Thoughts?
What are the emails sent out for? Because a CRON job would take time to do and when it ran, wouldn't it most likely slow up the entire server as well?
Send a message via MSN to Hershey Send a message via Skype™ to Hershey
Hershey is offline  
Reply With Quote
Old 11-20-2011, 09:02 PM   #5 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

I am just concious that i cant keep adding more and more properties its eventually going to get unworkable. Essentially wehen a holidaymaker submits a holiday enquiry i am asking in the code for an insert of around 60 lines to a couple of mysql tables and then called the php mail function to send aropund 60 emails. the databases IO runs very quickly but the php mail function takes an etrnioty to send out 60 emails. What happens when the 60 emails becomes 120. There must be an efficient way to process this i.e. send out 100 emails off the back of 1 form submit?
captainmerton is offline  
Reply With Quote
Old 11-28-2011, 11:21 AM   #6 (permalink)
The Visitor
 
Parkinson4's Avatar
 
Join Date: Nov 2011
Posts: 3
Thanks: 0
Parkinson4 is on a distinguished road
Default

Godaddy has the php.ini file configured with all the information.
This is the code to send a email using php is:
PHP code is:-

<?
include_once("Mail.php");
...

$to = "support@software506.com";
$subject = "Email from php";
$body = "Hi \n this is a test";

mail($to, $subject, $body);

...
this just works in the godaddy host, no for test locally.
it works for me.
just try it.

Last edited by Parkinson4 : 11-29-2011 at 11:32 AM.
Parkinson4 is offline  
Reply With Quote
Old 12-02-2011, 07:27 PM   #7 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Now implemented phpMailer. Still taking an eternity. Am i using this incorrectly? Its working fine but feels like it isnt running any quicker than the standard php mail() function. here my code:

I'm looping round 60 times and sending out the same email to several addresses. I had hoped that phpmailer would speed things up by opening the mail port once and not for every loop:

PHP Code:
<?php

    
class Message {

        public function 
__construct() {}

        
/* Send Email Message(s) */
        
public function sendMessage($subject=null,$recipients=null,$from=null,$content=null) {

            require_once(
'phpmailer.class.php');
    
             if (
is_array($recipients)) {
        
                foreach (
$recipients as &$email_recipient) {

                    If (
$this->send($email_recipient,$subject,$content,$from) == False) {
                        return 
false;
                    }
    
                }

                unset(
$email_recipient); // break the reference with the last element

            
} else {

                If (
$this->send($recipients,$subject,$content,$from) == False) {
                    return 
false;
                }

            }

        }


        
/* Send Email Message(s) */
        
private function send$recipient$subject$content$from ) {

            
$mail             = new PHPMailer(); // defaults to using php "mail()"

            
$body             $content;

            
$mail->AddReplyTo($from,$from);

            
$mail->SetFrom($from$from);

            
$mail->AddAddress($recipient$recipient);

            
$mail->Subject    $subject;

            
$mail->AltBody    "To view the message, please use an HTML compatible email viewer!"// optional, comment out and test

            
$mail->MsgHTML($body);

            
//$mail->AddAttachment("images/phpmailer.gif");      // attachment
            //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

            
if(!$mail->Send()) {
              
$this->failure True;
            }
            
            return 
True;

        }

    }

?>
captainmerton is offline  
Reply With Quote
Old 12-03-2011, 02:40 PM   #8 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Now found some tutorials so implemented the phpmailer class and changed my code to instantiate the class once then look on the mail send function and clear my address list each time. I've created a production proving enviroment and tested it thoroughly. Still it runs like a dog. If i remove the mail send element the mysql database read and writes run in milli seconds. Have now contacted my hosting company to understand how i can get a quicker mail server protocol performance.
captainmerton is offline  
Reply With Quote
Old 12-03-2011, 02:41 PM   #9 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Now found some tutorials so implemented the phpmailer class and changed my code to instantiate the class once then loop on the mailsend function and clear my address list each time. I've created a production proving enviroment and tested it thoroughly. Still it runs like a dog. If i remove the mail send element the mysql database read and writes run in milli seconds. Have now contacted my hosting company to understand how i can get a quicker mail server protocol performance.
captainmerton is offline  
Reply With Quote
Old 12-03-2011, 03:14 PM   #10 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

It will always take a few seconds per mail to send as the system has got to do a lookup on each domain, then send.. Also I'm not sure if it logs in with each mail turn on debuggind and watch what is happening... I have moved over to phpMailer after you mentioned how good it is and have found it takes around the same for me sendign the same quantity of mails...
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 12-03-2011, 03:18 PM   #11 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

For the debugging use :
Code:
$mail->SMTPDebug = 2;
0 = No output
1 = Minimal
2 = verbose
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 12-03-2011, 03:49 PM   #12 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

Just done a test on my server sending to 10 randomly generated recipients, so there is no spam panic, I created a catch all account on my server...

Here are the results I got :

SMTP -> FROM SERVER:220 <my server> ESMTP Postfix (Ubuntu)
SMTP -> FROM SERVER: 250-<my server> 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-AUTH LOGIN PLAIN 250-AUTH=LOGIN PLAIN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250-DSN 250 STARTTLS
SMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as D8AEA80A6EA

Sent individual email to no-one in particular in 0.71606993675232 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as 1E4F280A6EA

Sent individual email to no-one in particular in 0.21711301803589 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as 66E2280A6EA

Sent individual email to no-one in particular in 0.36099696159363 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as AB6AC80A6EA

Sent individual email to no-one in particular in 0.22245907783508 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as E188480A6EA

Sent individual email to no-one in particular in 0.47082090377808 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as 607B980A6EA

Sent individual email to no-one in particular in 0.24503397941589 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as 9C14280A6EA

Sent individual email to no-one in particular in 0.22583603858948 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as D354180A6EA

Sent individual email to no-one in particular in 0.21672320365906 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as 141F980A6EA

Sent individual email to no-one in particular in 0.21599817276001 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as 48F9380A6EA

Sent individual email to no-one in particular in 0.21703791618347 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as 7DFEF80A6EA

Sent individual email to no-one in particular in 0.21655082702637 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as B29E680A6EA

Sent individual email to no-one in particular in 0.2194459438324 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as E88C480A6EA

Sent individual email to no-one in particular in 0.22255706787109 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as 2A70D80A6EA

Sent individual email to no-one in particular in 0.2162709236145 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as 5F91A80A6EA

Sent individual email to no-one in particular in 0.22007513046265 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as 965B780A6EA

Sent individual email to no-one in particular in 0.24743604660034 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as D1D4780A6FD

Sent individual email to no-one in particular in 0.2244930267334 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as 1451480A6EA

Sent individual email to no-one in particular in 0.22491598129272 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as 4AD3680A6EA

Sent individual email to no-one in particular in 0.22699689865112 secondsSMTP -> FROM SERVER:250 2.1.0 Ok
SMTP -> FROM SERVER:250 2.1.5 Ok
SMTP -> FROM SERVER:354 End data with .
SMTP -> FROM SERVER:250 2.0.0 Ok: queued as 839BF80A6EA

Sent individual email to no-one in particular in 0.23592495918274 seconds
Overall time taken to send individual emails to no-one in particular in 5.3630180358887 seconds
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 12-03-2011, 03:52 PM   #13 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

So thats 5.4 seconds to 10 recipients, you are sending to 60, have you timed it ?
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 12-04-2011, 12:15 PM   #14 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Nope 60 doesnt timeout however it does take londer i'd say maybe 10 seconds so i am now thinking thats not too bad. I contacted my hosting company and they told me there are all kinds of restrictions on volume and frequency of sending emails so i need to be careful. I am thinking i will need to start investigating a better packahge perhaps moving to a virtual server and setting up a KRON job or something.

Maeltar - any guidance? Say for example i wanted to send 100 emails instantly when an enquiry form was submitted what is the best way to do this?

PS. Thanks for taking the time thus far to provide some feedback on this.
captainmerton is offline  
Reply With Quote
Old 12-04-2011, 01:35 PM   #15 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

I'd suggest getting a small VPS, you can get them form £10 a month, just configure it yourself, it's quite easy.. Or I can help you out with mail accounts and give you unlimited emails, no throttling etc..

If you go along the VPS route you will soon find just how much better having a dedicated server is than a hosted solution, I don't think I could manage without my dedi server now !

Most hosting companies do allow "cron" jobs, so it may be worth trying that route first...

As for VPS providors, http://www.budgetdedicated.com/ I found was reasonably priced, and if you go for the minimal setup (you won't be able to run Apache or MySQL) you will be able to set it up as a mail server..
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 12-05-2011, 08:07 PM   #16 (permalink)
The Visitor
 
Join Date: Dec 2011
Location: Vancouver, BC
Posts: 3
Thanks: 0
mfouwaaz is on a distinguished road
Default

Hi Captain Merton

Have you tried SplQueue or SplPriorityQueue? Looks like just what you need short of a cron job.

Hope this helps.
mfouwaaz is offline  
Reply With Quote
Old 12-06-2011, 06:18 PM   #17 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

What you need to do is make it so that PHP isn't waiting for a response if the emails have been sent. You won't be able to optimize the PHP to get a faster response since the bottle neck is the SMTP server.

You could have PHP execute scripts on the server that would send the emails and not block the PHP.

You could do a cron job. Not ideal since it won't be sending the emails right away. They will be delayed.

You could use AJAX and send 60 requests to a PHP file that would then send the emails asynchronously (you probably wouldn't want to send 60 requests at the same time). Each email being sent would then have its own process. But this could tie up apache processes and affect people on your site. This might be your only option since you said you are on a shared host.
__________________
Eric
wGEric is offline  
Reply With Quote
Old 12-10-2011, 09:29 AM   #18 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Thanks all for reponses and valuable insights/guidance. I have contacted my hosting company and find i do have CRON job functionality. I am investigating this. Meantime i have implemented phpMailer and added a "please wait" image using javascript when someone submits the form so they know something is happening other than just a hung browser.

wGEric your comment "You could have PHP execute scripts on the server that would send the emails and not block the PHP." - I'd be interested to understand what you mean? I assume php will send the mail to the mail server then await a response to see if it has actually worked (obviously in the php i am awaiting this response and will breake the send loop if its unfavourable). However are you saying somehow if i found a way not to wait for this response it would speed up i.e. send the mail then forget about it in the php so if it fails it fails?

Ewan
captainmerton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
need a built in function in php which work same as isinstance function of python tech Absolute Beginners 1 06-25-2009 01:22 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
Mail function.. Tanax Advanced PHP Programming 8 05-05-2008 08:20 PM


All times are GMT. The time now is 07:05 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design