Jump to content

[SOLVED] Email with conditional not working


Recommended Posts

Right now i have a email form that I want to email to certain people depending on their answer, such as if productinfo = chucks then email to bob and jerry, but if productinfo = rotarytables then email to bob and tim but not jerry...... I hope you could follow that :) anyways my code is below for the email.php which processes the form.

 

I have tried two ways doing this to show that I am trying,  so maybe you can take a look at both and tell me where I am going wrong?

 

One more thing from what I am noticing is that it's sticking on the first conditional statement and never acts to check if there are more conditions to be met so I know my code sucks now .

 

 

PHP email.php version 1

<?php 
if($productinfo = 'rotarytables') { 
$to = ' [email protected], [email protected]';
$subject = "x.Net Contact Us from $fullname";
$message = "<p>stuff here</p>"; // must be double quoted to be able to use variables inside a string
$headers = "From: [email protected]\r\n" .
	   'X-Mailer: PHP/' . phpversion() . "\r\n" .
	   "MIME-Version: 1.0\r\n" .
	   "Content-Type: text/html; charset=utf-8\r\n" .
	   "Content-Transfer-Encoding: 8bit\r\n\r\n";

} 

elseif($productinfo = 'chucks') { 
$to = ' [email protected], [email protected]';
$subject = "x.Net Contact Us from $fullname";
$message = "<p>Stuff here</p>"; // must be double quoted to be able to use variables inside a string
$headers = "From: [email protected]\r\n" .
	   'X-Mailer: PHP/' . phpversion() . "\r\n" .
	   "MIME-Version: 1.0\r\n" .
	   "Content-Type: text/html; charset=utf-8\r\n" .
	   "Content-Transfer-Encoding: 8bit\r\n\r\n";
}

else { 	

// Send
$sent = mail($to, $subject, $message, $headers) ; 
if($sent) 
{print "<center><p><span class=\"bold\">Thank you $fullname! Your mail was sent successfully, the details are as followed</span></p></center>"; }
else 
{print "<p><span class=\"bold\">We encountered an error sending your mail, please go back to the <a href=\"contact.php\">contact form</a></span></p>"; }

}
?> 

 

 

email.php version 2

 

<?php 
if($productinfo = 'rotarytables') { 
$to = ' [email protected], [email protected]';
$subject = "x.Net Contact Us from $fullname";
$message = "<p>Stuff here</p>"; // must be double quoted to be able to use variables inside a string
$headers = "From: [email protected]\r\n" .
	   'X-Mailer: PHP/' . phpversion() . "\r\n" .
	   "MIME-Version: 1.0\r\n" .
	   "Content-Type: text/html; charset=utf-8\r\n" .
	   "Content-Transfer-Encoding: 8bit\r\n\r\n";

// Send
$sent = mail($to, $subject, $message, $headers) ; 
if($sent) 
{print "<center><p><span class=\"bold\">Thank you $fullname! Your mail was sent successfully, the details are as followed</span></p></center>"; }
else 
{print "<p><span class=\"bold\">We encountered an error sending your mail, please go back to the <a href=\"contact.php\">contact form</a></span></p>"; }
} 

elseif($productinfo = 'chucks') { 
$to = ' [email protected], [email protected]';
$subject = "x.Net Contact Us from $fullname";
$message = "<p>Stuff Here</p>"; // must be double quoted to be able to use variables inside a string
$headers = "From: [email protected]\r\n" .
	   'X-Mailer: PHP/' . phpversion() . "\r\n" .
	   "MIME-Version: 1.0\r\n" .
	   "Content-Type: text/html; charset=utf-8\r\n" .
	   "Content-Transfer-Encoding: 8bit\r\n\r\n";

// Send
$sent = mail($to, $subject, $message, $headers) ; 
if($sent) 
{print "<center><p><span class=\"bold\">Thank you $fullname! Your mail was sent successfully, the details are as followed</span></p></center>"; }
else 
{print "<p><span class=\"bold\">We encountered an error sending your mail, please go back to the <a href=\"contact.php\">contact form</a></span></p>"; }
} 

?> 

 

Let me know if I am not clear in my question and I 'll try my best to give you the information  you may need, other than that, thanks a ton for taking a look at my code to teach me what I am doing wrong.

in the first example, why are you using an else to send the mail? if you take the send mail part out of the else it will work.

 

in the second example, it looks fine as far as i can tell. the only thing i can suggest is check your headers.

Thanks vine, that was exactly it, I honestly do not know why they were there, I guess that's what I get when I am being lazy and do not really think things through, I got it working now. Thanks abazoskib for your answer as well in the reason why each example would not work.

 

Thanks again guys.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.