problem sending mail using php

I have used successfully the following php script to send mail using html form. As you can see the following script accommodates only three entries namely name, email and comments.

But when I try to increase the number of entries to say four making required changes in the script, I get nowhere. I need someone’s feedback so I can understand how to go about correcting this situation. I can upload the html form if he or she would require it.

Thanks.

Bob Ghodsi

<

Emailing Form Data code {color:#F00C4D;font-weight:bold;font-size:1.2em} i {color: #6D0CF0}

.error { color: red; }

<?php

$name=$_POST[‘name’];
$email=$_POST[‘email’];
$comments=$_POST[‘comments’];

$redirectTo = “http://www.bghodsi.com”;
$to = "[email protected]";
$from = "[email protected]";
$subject = “Contact Form Submission”;
$headers = “From: $from\r\n”;

$message = “”;
$formFields = array_keys($_POST);

for ($i = 0; $i < sizeof($formFields); $i++)
{
$theField = strip_tags($formFields[$i]);
$theValue = strip_tags($_POST[$theField]);
$message .= $theField;
$message .= " = ";
$message .= $theValue;
$message .= “\n”;
}
// Check if the form has been submitted
if (isset($_POST[‘submitted’])) {

 $problem = FALSE ;// No problems so far.

// Check for each field

 if (empty($_POST['name'])) {
 		$problem = TRUE;
	 
		}
		
		if (empty($_POST['email'])) {
 		$problem = TRUE;
		 
		}
		if (empty($_POST['comments'])) {
 		$problem = TRUE;
		 
		}

if ($problem)
{
include(“oops.php”);
exit;
}
else {

			  $success = mail($to, $subject, $message, $headers); 

include(“thanks.php”);

}
} ?>

</html

Well, that is simple enough. First a couple of things.
Please place your code inside of the PHP tags so we can copy it easier.
Just press the PHP button and insert your code between the tags.

Next, you showed the email code, but, not the form code. You did not show where this data is coming from, so I will assume you cut that part out. What are you trying to add?

Lastly, the way that your code captures data is messed up a bit. It is pulling all of your form data not just the three or four you mentioned. That is most likely not the way to go as it will send hidden items, too. You only want protected data to be sent in an email.

So, show us the form code that goes with this email code and we can fix you up…

[php]<?php
session_start();

if (isset($_POST[‘answer’])) {
$computer = htmlspecialchars($_POST[‘answerA’]) + htmlspecialchars($_POST[‘answerB’]);
if ( $computer != htmlspecialchars($_POST[‘answer’])) {
$_SESSION[‘errMsg’] = ‘Incorrect Answer, Try Again!’;
header(‘Location:contact.php’);
exit();
}
}

require ‘lib/PHPMailer/PHPMailerAutoload.php’;

$mail = new PHPMailer;

$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ‘smtp.example.com’; // Specify main and backup server
//$mail->Port = 587; // Comment out if wanting to send via server (local server only)
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ‘[email protected]’; // SMTP username
$mail->Password = your_password’; // SMTP password
$mail->SMTPSecure = ‘tls’; // Enable encryption, ‘ssl’ also accepted

$mail->From = $_POST[‘email’];
$mail->FromName = $_POST[‘name’];
$mail->addAddress(‘[email protected]’, ‘John Doe’); // Add a recipient
//$mail->addAddress(‘[email protected]’); // Name is optional
$mail->addReplyTo($_POST[‘email’], ‘Information’);
//$mail->addCC(‘[email protected]’);
//$mail->addBCC(‘[email protected]’);

$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment(’/var/tmp/file.tar.gz’); // Add attachments
$mail->addAttachment(‘lib/images/img-logo.png’, ‘logo.png’); // Optional name
$mail->isHTML(true); // Set email format to HTML

$mail->Subject = strip_tags($_POST[‘subject’]);
$mail->Body = ‘

’ . $_POST[‘comment’] . ‘

’;
$mail->AltBody = $_POST[‘comment’];

if(!$mail->send()) {
echo ‘Message could not be sent.’;
echo 'Mailer Error: ’ . $mail->ErrorInfo;
exit;
}
$_SESSION[‘errMsg’] = ‘Email Successfully Sent!’;
header(‘Location: contact.php’);
exit();[/php]

In my opinion I would use a trusted php email script (client), I for one use PHPMailier (You can find it by many methods). One it’s easy to configure, two it’s easy to test on a local server and lastly it pretty darn secure. Like I said that’s my opinion, but you know what they say about opinions… ;D Why re-invent the wheel when you don’t have to?

Strider, I agree with you. I just didn’t think he understands this code and how it works, so a new library might be overkill for him. But, yes for people who do a lot of mailing something like PHPmailer is great!

Sponsor our Newsletter | Privacy Policy | Terms of Service