Avatar of rinfo
rinfo

asked on 

php mail fails on subsequent mail sending

I have used following codes for sending mail to user and admin.
First code is for users and it works ok.
Only problem is it sometime lands on junk/spam.
However when it sends mail to admin after sending mail to user - there is no error notified.
But admin is not recieving mail.
I have checked message body is properly populated.
Email provided is valid and working.
Can someone please tell me where i am wrong and what measure i have to take to prevent user mail going to junk.
Thanks
$email_from = 'webadminmailid@hotmail.com';
            $email_message ='
            <html>
            <body>
             message body here
            </body>
            </html>
           
            


			$headers  = "MIME-Version: 1.0" .  "\r\n";
                    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";	
			$headers .= ' From: '.'mymailid@hotmail.com'."\r\n" .
                           'Reply-To: '.$email_from.'\r\n' .
                           'X-Mailer: PHP/' . 'phpversion()';
			@mail($email,'Thank-you', $email_message, $headers);

			
			
			

			$headers1 = '';
			$emailto  = "adminmailid@gmail.com";
	        $email_from1 = $_POST['email'];
	        $email_message1 = "A new user having following details has registered on ".date("Y-m-d")." at ".date("H:i")."\n\n";
	        foreach($_POST as $field_name => $field_value) {
	        $email_message1.= $field_name.": ".$field_value."\n\n";
	        }
			$headers1 = 'MIME-Version: 1.0' . "\r\n";
			$headers1 .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	        $headers1 .= 'From: '.$email_from1."\r\n" .
                           'Reply-To: '.$email_from1."\r\n" .
                           'X-Mailer: PHP/' . phpversion();
                // send the email
			
            mail($emailto,"New Registration", $email_message1, $headers1) ;
            if($mail->ErrorInfo) { die($mail->ErrorInfo) ; }   
				
		header("location: register-success.php");

Open in new window

Scripting LanguagesPHP

Avatar of undefined
Last Comment
Ray Paseur
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

It is a plain fact that you cannot be using that script to send email, since the script fails with a parse error.  When you want to get help with a working script, it's usually a good idea to post the script "as-is" instead of redacting it.  When scripts are shortened like this one, there is often important information that is lost in the cut-and-paste process.
what measure i have to take to prevent user mail going to junk.
Let's start with that one.  You should tell your community to "whitelist" the sending email address and domain.  Second, and this is more art than science, you should not send email messages that look like spam.  Use the client's name in the email message body.  Avoid sending a generic subject string like 'Thank-you' -- that is a sure signal for spam filters.  Avoid sending from a generic name at hotmail.com -- another sure signal for spam filters.  Instead, put meaningful information into the subject and use a genuine email address at your domain name.  You might also want to omit 'X-Mailer: PHP/' . 'phpversion()'; since that is a clear signal that this is an automated message (all spam messages are automated messages, even though not all automated messages are spam).  And finally, check the headers by sending one of the messages to yourself at your own Gmail account.  You can "show original" to look at the raw email message including all the headers.  You will be able to see if your hosting company is adding anti-spam messages to your outgoing email.  If they are doing that, you might want to consider using PhpMailer instead of mail().

In the case of the admin emails, there is not much I can tell you because the content of the message relies on external data and we cannot see that data.  However you can omit this part: if($mail->ErrorInfo) { die($mail->ErrorInfo) ; } because that relies on object-oriented notation, and there are no instances of the mail object in the code.  Instead, I would look at the code logic carefully (requires the full script!) and see if there is not some conditional if() structure or similar that is blocking access to the part of the code that sends the admin mail.  I would also remove the redirection at the bottom and instead use var_dump() to print out all of the variables involved in the admin mail.  Once you see the script logic and the content of the variables you will probably be able to see what's going awry.

See also http://php.net/manual/en/function.mail.php and read it with an eye for detail.

HTH, ~Ray
Avatar of rinfo
rinfo

ASKER

Hello Mr.Ray
Thanks for the insights and i appreciate that.
It is a plain fact that you cannot be using that script to send email, since the script fails with a parse error.
Maybe it did have parse error because i posted a part of the code.
But i can assure you its a fully working code and as i mentioned there is no problem in sending the first mail its receipt ok by the user.
However exactly same code when used to send information to admin fails and i am saying is failing because i am not receiving this mail .
My confusion is why first mail works ok and second one fails.
I am not using generic email like admin mail etc , its more like individual name like james.watt@gmail.com.I would try and use domain email id for admin mail.
In the case of the admin emails, there is not much I can tell you because the content of the message relies on external data and we cannot see that data.
I have var_dumped on two variables $messages ($messages  consist of data from post values ),$headers they are populated as intended and i can see the values there is nothing wrong in that.
I would post the entire php script once i am on my workstation.
Avatar of jmdl1983
jmdl1983

Do you have the
header("location: register-success.php");

Open in new window

run after each email?

You can only specify header location once if so.
Avatar of rinfo
rinfo

ASKER

No i am redirecting only after the second mail and is not used for first mail.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I would post the entire php script once i am on my workstation.
That will be very helpful because then we will have the true line numbers and can give some step-by-step directions to help debugging.

Also, please refer to my profile page here, click the "About me" tab and find my email address.  You can put my email address in as the admin email.  I will post the entire contents of the test message you send me and we can look at the headers.
Avatar of rinfo
rinfo

ASKER

Yeah that sure would be a good plan.
I would register you as a user as well as mention your id in the admin mail.
But problem is admin are not recieving email and without email how can you see headers.
Maybe user mail that you would recieve could give you a clue.
BTW i have a doubt could it because in the first mail header is set and in subsequent mail it might be experiencing header already sent or something like that.
Besides can you suggest me some code that would be used for both the mail one after another. Some how or other i feel that if entire code sent from another script will work if it is sent alone.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

admin are not recieving email and without email how can you see headers
What we need to understand is why the admin is not receiving the email.  If you put my address in for the admin in your test case, I will be able to see once we get the email to work!  I do not need to see the user mail, only the admin email.

"Header already sent" has to do with HTTP protocol, not email.  It is not an issue in this script.
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

You have several problems that I can see in addition to the script being incomplete.  If you are sending email 'from' a Gmail or Hotmail address and you are not sending thru those services, the receiving email client can tell that and some will reject your email because you are trying to send from an email address that is not valid from your server.  When I send email from my web site, I always send from an email address that is valid on my server.  Some services actually check to see if your email address is valid as well as checking to see if it matches your server.
Avatar of rinfo
rinfo

ASKER

Hello Dave
Thanks for the input.One of the reason i used hotmail because client has insisted that they would rather use their regular hotmail id for the admin mail as they seldom check their domain mail service despite my insistance that they should.

You have several problems that I can see in addition to the script being incomplete
I will be posting the entire script . Please do let me know what are the other problems besides using generic email id .
Avatar of rinfo
rinfo

ASKER

Hello Dave
Sorry for the haste reply. I understand you mentioned from mail id should be rather server mail id and not admin email id . And that should not be a problem with the clients since they will be using their hotmail to recieve email (to admin mail id).
Point noted and understood. I will implement that.
Avatar of rinfo
rinfo

ASKER

Here is the php script file i am using.
Please have a look at suggest me anything that you should find for making it a better and working script.
I am open to all suggestion.
Thanks
BTW Merry Christmas and hope i am not bothering you on your festivity.
register-exec.php
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

@rinfo: Thanks for the Christmas wishes.  All the rest of my family is still asleep this morning, so we are enjoying the holidays each in our own ways!
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of rinfo
rinfo

ASKER

Yes its working ok now .
I did made changes as suggested by Mr.Ray.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Glad things are looking up!  Thanks for using EE, and Happy New Year 2013!
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo