Jump to content

Sending 2 emails in one script


Love2c0de

Recommended Posts

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 by Love2c0de
Link to comment
Share on other sites

 


 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...)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Good afternoon guys,

 

I'm torn on what to do, you have completely different opinions......discuss more please!  :happy-04:

 

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 by Love2c0de
Link to comment
Share on other sites

 

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 :confused:

 

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 by jazzman1
Link to comment
Share on other sites

 


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.

Link to comment
Share on other sites

@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!

 

 

Link to comment
Share on other sites

 


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.

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

 


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.

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 


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.

Link to comment
Share on other sites

 

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 by jazzman1
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

 

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! 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.