Reputation:
I have created a PHP script to send emails from my website to my support email address. The script is using PHPMailer but it seems to have a bug when sending a email as its returning false like it was unable to send the email but is sending it with no problems, here is my PHP code...
<?php
header('application/json');
require_once('phpmailer.php');
// Retreive all the required fields
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
$timestamp = date("dmYhms", mktime(date(h), date(m), date(s), date(m), date(d), date(y)));
// Get users ip address
if ($_SERVER['HTTP_X_FORWARD_FOR'] != '') {
$ipaddress = $_SERVER['HTTP_X_FORWARD_FOR'];
} else {
$ipaddress = $_SERVER['REMOTE_ADDR'];
}
// Check a name was specified
if ($name == ''){
$output = json_encode(array('status'=>'invalid','message'=>'Please enter your full name.'));
die($output);
}
// Check a email address was specified
if ($email == ''){
$output = json_encode(array('status'=>'invalid','message'=>'Please enter your email address.'));
die($output);
}
// Check a subject was specified
if ($subject == ''){
$output = json_encode(array('status'=>'invalid','message'=>'Please enter a subject for your message.'));
die($output);
}
// Check a message was specified
if ($message == ''){
$output = json_encode(array('status'=>'invalid','message'=>'A message without a message, interesting...'));
die($output);
}
// Double check all the fields are not blank and prepare the message
if ($name != '' && $email != '' && $subject != '' && $message != ''){
// Check the email address specified is valid
if (preg_match("/^(\w+((-\w+)|(\w.\w+))*)\@(\w+((\.|-)\w+)*\.\w+$)/", $email)){
$mail = new phpmailer;
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress("[email protected]", "IDify Ltd");
$mail->AddReplyTo("[email protected]", "IDify Ltd");
$mail->WordWrap = 500;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = "<strong>Ip Address: </strong>".$ipaddress."<br/><strong>Timestamp: </strong>".$timestamp."<br/><strong>Fullname: </strong>".$name."<br/><strong>Email Address: </strong>".$email."<br/><strong>Subject: </strong>".$subject."<br/><p>".$message."</p>";
if($mail->Send()){
$output = json_encode(array('status'=>'valid','message'=>'Your message is on its way, we will be in touch with you soon.'));
die($output);
} else {
$output = json_encode(array('status'=>'invalid','message'=>'We were unable to send your message, please try again later.'));
die($output);
}
}else{
$output = json_encode(array('status'=>'invalid','message'=>'Please enter a valid email address.'));
die($output);
}
}
?>
Thanks for any help given, its very strange but I'm sure we can work it out.
Upvotes: 2
Views: 3426
Reputation: 9752
Just a note about the code before I take a stab at the problem, I would not bother verifying the email with a regular expression. While there is an RFC standard for doing so many mail servers have different implementations and I have found that simply trying to send the email is better than bouncing out potentially valid email addresses.
And as far as it returning False do you mean that $mail->Send()
returns False?
I took a look at the PHPMailer source code:
545 public function Send() {
SNIP SNIP
581 } catch (phpmailerException $e) {
582 $this->SetError($e->getMessage());
583 if ($this->exceptions) {
584 throw $e;
585 }
586 echo $e->getMessage()."\n";
587 return false;
588 }
589 }
If the send function is throwing an exception inside the switch statement in line 572 then you could have a case where the mail is sent however you hit the catch block and return False.
If this is the case then there may be an issue with your configuration, would you be willing to try an example script from the project and report the results of that?
Upvotes: 2