Love2c0de Posted September 19, 2013 Share Posted September 19, 2013 (edited) Good evening, Writing an email script which will send an email to the client saying thank you for contact plus their message below including the time they sent the message. The second email will be one going to myself so I am notified about the contact, again containing the message. I want to just reset the $to and $subject variable values and re use them. Is proper practice to unset the variables then recreate them, or just set them to an empty string before writing the second script? Thanks for the advice. Kind regards, L2c. Edited September 19, 2013 by Love2c0de Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/ Share on other sites More sharing options...
fastsol Posted September 19, 2013 Share Posted September 19, 2013 You don't have to unset them nor set them to nothing value. Just reassign their value directly when you need. Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450286 Share on other sites More sharing options...
Love2c0de Posted September 19, 2013 Author Share Posted September 19, 2013 Wow indeed. What the hell am I thinking? Kind regards, Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450288 Share on other sites More sharing options...
vinny42 Posted September 19, 2013 Share Posted September 19, 2013 Just reassign their value directly when you need. Just make *very* sure that you fill all variables with new data, you don't want to send thw wrong emails to the wrong people. And for that reason it might be a good idea to put the two email routines in separate functions, so there is no possibility of cross contamination. (it's no big deal for your private website, but accidently sending a log of a serious exception to a randmon visitor who was trying to subscribe to a mailinglist is something else...) Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450312 Share on other sites More sharing options...
Love2c0de Posted September 19, 2013 Author Share Posted September 19, 2013 I will take that into consideration. I'm using a separate file with a require statement in my main index.php file after processing the data. Here is the file: index.php if(!isset($error)) { //no errors, we have required data - continue $date = date("Y-m-d", time()); require("$dir/send_emails.php"); require("$dir/$dirq/insert_contact.php"); } The email script: <?php //write email scripts //client script $to = $_POST['email']; $subject = "TSPV-Websites"; $message = "Thank you for your contacting us.\r\n<br />"; $message .= "We will reply to you shortly.\r\n\r\n<br /><br />"; $message .= "Please find your message details below for reference:\r\n\r\n<br /><br />"; $message .= "<b>Your name:</b> ".$_POST['name']."\r\n<br />"; $message .= "<b>Your Number:</b> ".$_POST['phone']."\r\n<br />"; $message .= "<b>Your Email:</b> ".$_POST['email']."\r\n<br />"; $message .= "<b>Your Referrer:</b> ".$_POST['referrer']."\r\n<br />"; $message .= "<b>Your Comments:</b> ".$_POST['comments']."\r\n\r\n<br /><br />"; $message .= "<b>Message Sent:</b> ".$date."\r\n<br />"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= "From: enquiries@tspv-websites.co.uk"; mail($to,$subject, $message,$headers); //admin script $to = "enquiries@tspv-websites.co.uk"; $subject = "TSPV-Websites"; $message = "We have received contact from a user..\r\n<br />"; $message .= "Please find the message details below for reference:\r\n\r\n<br /><br />"; $message .= "<b>Their name:</b> ".$_POST['name']."\r\n<br />"; $message .= "<b>Their Number:</b> ".$_POST['phone']."\r\n<br />"; $message .= "<b>Their Email:</b> ".$_POST['email']."\r\n<br />"; $message .= "<b>Their Referrer:</b> ".$_POST['referrer']."\r\n<br />"; $message .= "<b>Their Comments:</b> ".$_POST['comments']."\r\n\r\n<br /><br />"; $message .= "<b>Message Sent:</b> ".$date."\r\n<br />"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= "From: admin@tspv-websites.co.uk"; mail($to,$subject, $message,$headers); ?> Regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450328 Share on other sites More sharing options...
jazzman1 Posted September 20, 2013 Share Posted September 20, 2013 In this particular case, I would suggest you to call the mail function twice using some sleep functions between sending_to_user and sending_to_admin. Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450333 Share on other sites More sharing options...
vinny42 Posted September 20, 2013 Share Posted September 20, 2013 using some sleep functions between sending_to_user and sending_to_admin. You don't have to sleep between sending two emails.... that's just silly. If you are sending hundreds of emails then yes, leave some time for the server to catch up, but two emails... Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450356 Share on other sites More sharing options...
Love2c0de Posted September 20, 2013 Author Share Posted September 20, 2013 (edited) Good afternoon guys, I'm torn on what to do, you have completely different opinions......discuss more please! I mean, the way I have it right now, it's working perfectly. I used one of my old email accounts (as a user) and use enquiries@tspv-websites.co.uk to inform me of contact. I'm receiving both emails saying the correct things. Discuss more though because I'd like it to be as robust as possible with no room for error. Kind regards, L2c Edited September 20, 2013 by Love2c0de Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450370 Share on other sites More sharing options...
jazzman1 Posted September 20, 2013 Share Posted September 20, 2013 (edited) You don't have to sleep between sending two emails.... that's just silly. If you are sending hundreds of emails then yes, leave some time for the server to catch up, but two emails... Why do you call my answer as silly I mean to pause the script for a few seconds. It's always a good way to ensure that mail has completely terminated before calling next one. Edited September 20, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450372 Share on other sites More sharing options...
vinny42 Posted September 20, 2013 Share Posted September 20, 2013 I mean to pause the script for a few seconds. It's always a good way to ensure that mail has completely terminated before calling next one. Who told you that? :-) mail() contacts the local delivery service and returns true or false when it has completed, so PHP cannot continue until mail() has completed. You cannot send the next message before the last one has completely finished, so your sleep command cannot be executed before mail() has finished. There is no point in waiting for a finished proces to finish. Worst case, if you send thousands and thousands of emails, the local delivery will throttle you to give itself time to pass the messages on to the internet, which makes mail() return false to indicate that the mail has not been sent. In that case, and pretty much only in that case, do you have to wait for the mailserver to catch up. But when sending two emails there is no need whatsoever to wait for any amount of time. Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450374 Share on other sites More sharing options...
jazzman1 Posted September 20, 2013 Share Posted September 20, 2013 @vinny42, I think you are little confused, what a mail function it is and what it does.The big problem is, that this function as you said above, it contacts the local delivery service by default it's sendmail. So, the mail function doesn't care and doesn't know what delivery report comes back to the mail server. But in fact that sendmail is a complex software. It provides and supports many kinds of mail-transfer and delivery methods. Sometimes, we're sending a lot of body content, binary data, html email templates and so on, so on and it's always good practice to give sendmail a little time, when we call it twice or more times. Who told you that? :-) Everyone, who had a touch with mail servers. Worst case, if you send thousands and thousands of emails, the local delivery will throttle you to give itself time to pass the messages on to the internet, which makes mail() return false to indicate that the mail has not been sent. No, I have to disagree with you. Even the mail server is stopped, the mail function would be return true! But when sending two emails there is no need whatsoever to wait for any amount of time. It depends of the size of the message. I don't want to say that Love2c0de will have a problem here......but I would not say that my suggest is silly! Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450390 Share on other sites More sharing options...
vinny42 Posted September 20, 2013 Share Posted September 20, 2013 The big problem is, that this function as you said above, it contacts the local delivery service by default it's sendmail. So, the mail function doesn't care and doesn't know what delivery report comes back to the mail server. But in fact that sendmail is a complex software. It provides and supports many kinds of mail-transfer and delivery methods. Sometimes, we're sending a lot of body content, binary data, html email templates and so on, so on and it's always good practice to give sendmail a little time, when we call it twice or more times. Sendmail, like every other mailserver software, uses a queueing system and a processor. The queue accepts new messages while the processor is sending previously queued messages to their destination. The key thing to note is that these processes work simultaneously. The fact that mail() returns true does not mean that the server is now working to send the message. Your message is in the queue and will be processed when the server sees fit. In the meantime the queue is still listening and accepting messages. Now, even though sending a message could require some work (it really isn't much, SMTP is frighteningly simple), queueing a message requires only a very basic format check that simply doesnt put any load on the server. Moreover, mailservers can be configured to always keep a certain amount of resources dedicated to the sender so even if you manage to max-out the queueing process that still doesn't affect the sending of messages. No, I have to disagree with you. Even the mail server is stopped, the mail function would be return true! I highly doubt that, but then again mail() is a crappy function. It's usually better to use something like phpmailer or swiftmailer, they use sockets to connect so if a server is down there will be an error. Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450428 Share on other sites More sharing options...
jazzman1 Posted September 20, 2013 Share Posted September 20, 2013 Sendmail, like every other mailserver software, uses a queueing system and a processor. One more reason to avoid looping the mail function. The sleep function prevent the loop It's usually better to use something like phpmailer or swiftmailer, they use sockets to connect so if a server is down there will be an error. Are you sure that swiftmailer provides a mechanism to check if the mail server is down? I'll check that later at home. Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450441 Share on other sites More sharing options...
vinny42 Posted September 20, 2013 Share Posted September 20, 2013 One more reason to avoid looping the mail function. The sleep function prevent the loop What do you mean by "avoid looping"? The queue is the single reason why it really doesn't matter how fast or slowly you queue the messages. Are you sure that swiftmailer provides a mechanism to check if the mail server is down? I'll check that later at home. It doesn't have to check if the server is down, because it must connect in order to queue the mail. If the server is not responding the mail cannot be queued and that must result in an errormessage. Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450445 Share on other sites More sharing options...
jazzman1 Posted September 20, 2013 Share Posted September 20, 2013 What do you mean by "avoid looping"? The queue is the single reason why it really doesn't matter how fast or slowly you queue the messages. I mean, the queue is a mechanism providing by sendmail to address outgoing email messages. The queue is not a part of the server. When you call two or more times sendmail, this daemon is being terminated twice or more. If the server is not responding the mail cannot be queued and that must result in an errormessage. I'll check that. Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450451 Share on other sites More sharing options...
Love2c0de Posted September 21, 2013 Author Share Posted September 21, 2013 Good morning, Great topic and good to read. The emails are not that significant as I log into my database everyday so should someone contact me and the email fails, chances are I will spot the contact within my database where I send all their data. Ideally though I would like an email to notify me. At the minute it works perfect but I'm open to any suggestions to improve it. Is using an external mailing system necessary for this occasion? Kind regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450502 Share on other sites More sharing options...
vinny42 Posted September 21, 2013 Share Posted September 21, 2013 When you call two or more times sendmail, this daemon is being terminated twice or more. Deamons don't terminate, that's the point of a deamon. But still, how would waiting help? Ideally though I would like an email to notify me. At the minute it works perfect but I'm open to any suggestions to improve it. Is using an external mailing system necessary for this occasion? You are not sending bulk emails, your hoster will have no problem handling them, so just go for it. If you still have doubts, contact your hoster and explain what you want to do. They will tell you what they thinks is best. Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450534 Share on other sites More sharing options...
jazzman1 Posted September 21, 2013 Share Posted September 21, 2013 (edited) If the server is not responding the mail cannot be queued and that must result in an errormessage. No, even the mail server is being down, the mail can be queued and it will addressed those outgoing email messages when it is UP again. Deamons don't terminate, that's the point of a deamon. Yes, it's my fault. I know what daemon does.You perfectly understand what I wanted to say Edited September 21, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450558 Share on other sites More sharing options...
vinny42 Posted September 21, 2013 Share Posted September 21, 2013 No, even the mail server is being down, the mail can be queued and it will addressed those outgoing email messages when it is UP again. That depends very much on how you connect to the server, and also, who cares if the sender is down, if the message has been queued it will be sent at some point. Yes, it's my fault. I know what daemon does.You perfectly understand what I wanted to say Actually no, the queue really doesnt care if you wait or not. Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450562 Share on other sites More sharing options...
jazzman1 Posted September 21, 2013 Share Posted September 21, 2013 That depends very much on how you connect to the server No, actually it doesn't really matter what type of connection you're using. who cares if the sender is down, if the message has been queued it will be sent at some point. I do care! Actually no, the queue really doesnt care if you wait or not. I'm not talking about the queue in sendmail here. You want to run the same program twice or more on the same time - this is a problem! Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450565 Share on other sites More sharing options...
vinny42 Posted September 21, 2013 Share Posted September 21, 2013 Sigh. Whatever. I'm done arguing with you. Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450567 Share on other sites More sharing options...
Love2c0de Posted September 21, 2013 Author Share Posted September 21, 2013 Look at what I've caused I'll stick with what I have for these purposes as it works and it's only 2 emails so it should suffice I think. Thanks for all the input. Kind regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/282289-sending-2-emails-in-one-script/#findComment-1450570 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.