0

Tried to send an email get no errors, but no email is sent? Can I please have some assistance? I'm trying to do make a contact form that can send emails for a college project, as that is one of the requirements to fulfill the criteria. Php code has been added, i followed a tutorial to create that code and the contact form, which can be found here: https://www.youtube.com/watch?v=1CkBsGhux9U

PLEASE REMOVE DUPLICATION

php.ini

[mail function]
;For Win32 only.
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = rudolf226@gmail.com
sendmail_path = "E:\stone\xampp2\sendmail\sendmail.exe" -t"`

For Win32 only.
; http://php.net/sendmail-from '
sendmail_from = rudolf226@gmail.com   

sendmail:

[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=rudolf226@gmail.com
auth_password=password
force_sender=rudolf226@gmail.com
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = rudolf226@gmail.com

php code:

<?php 
// define variables and set to empty values
$name_error = $email_error = $phone_error = $url_error = "";
$name = $email = $phone = $message = $url = $success = "";


//form is submitted with POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $name_error = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $name_error = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["email"])) {
    $email_error = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $email_error = "Invalid email format"; 
    }
  }

  if (empty($_POST["phone"])) {
    $phone_error = "Phone is required";
  } else {
    $phone = test_input($_POST["phone"]);
    // check if e-mail address is well-formed
    if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
      $phone_error = "Invalid phone number"; 
    }
  }

  if (empty($_POST["url"])) {
    $url_error = "";
  } else {
    $url = test_input($_POST["url"]);
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$url)) {
      $url_error = "Invalid URL"; 
    }
  }

  if (empty($_POST["message"])) {
    $message = "";
  } else {
    $message = test_input($_POST["message"]);

  }


  if ($name_error == '' and $email_error == '' and $phone_error == '' and $url_error == '' ){
      $message_body = '';
      unset($_POST['submit']);
      foreach ($_POST as $key => $value){
          $message_body .=  "$key: $value\n";
      }

      $to = 'rudolf226@gmail.com';
      $subject = 'Contact Form Submit';
      if (mail($to, $subject, $message_body)){
          $success = "Message sent, thank you for contacting us!";
          $name = $email = $phone = $message = $url = '';
      }
  }

}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
  • 1
    First can you try to find some logs to check if they show any info? Or within your code when you send mail, dump out any response or enable debug. Can you show us the PHP code where you sending the email? – Giedrius Aug 29 '17 at 16:08
  • not too sure where to look sorry – liam donnelly Aug 29 '17 at 16:10
  • 1
    This might get you started (to find logs): https://stackoverflow.com/questions/3719549/where-does-phps-error-log-reside-in-xampp Also please include any code you use to send the email, currently everything is very unclear. – Giedrius Aug 29 '17 at 16:11
  • i'll add the php code though – liam donnelly Aug 29 '17 at 16:11
  • I'm pretty sure the error logs are just referring to me trying to install phpmailer when i couldnt get this to work, but i'll add them. – liam donnelly Aug 29 '17 at 16:17
  • It might be easier using some library (like you mentioned `PHPMailer`) to send emails (especially if you want to deal with attachments and stuff later), as they will do a lot of work and configuration for you. – Giedrius Aug 29 '17 at 16:19
  • what else do you need if anything? – liam donnelly Aug 29 '17 at 16:19
  • phpmailer didn't work for me – liam donnelly Aug 29 '17 at 16:20
  • Your error log refers to `PHPMailer` but I don't see you including or using it anywhere in your code. Also you got some syntax errors, can you check those and make sure you only include log entries related to your issue? – Giedrius Aug 29 '17 at 16:22
  • Like i said i tried phpmailer after i couldnt get this to work, so that's why. – liam donnelly Aug 29 '17 at 16:23
  • If you want help with `PHPMailer` solution, post code for that. If not, then filter out the log entries related to your current solution. Also try this to see if you can spot what the error is with your mail function: https://stackoverflow.com/questions/3186725/how-can-i-get-the-error-message-for-the-mail-function – Giedrius Aug 29 '17 at 16:27
  • i just posted what you told me to post... Okay, 1 sec. – liam donnelly Aug 29 '17 at 16:30
  • Nevermind none of these logs are relevant i just tried to send it again and nothing came up – liam donnelly Aug 29 '17 at 16:34
  • Again, follow the accepted answer on this: https://stackoverflow.com/questions/3186725/how-can-i-get-the-error-message-for-the-mail-function and apply to yourself to see any errors with your `mail` function. – Giedrius Aug 29 '17 at 16:37
  • Sorry not sure where to put this – liam donnelly Aug 29 '17 at 16:43
  • Looks useful though lol – liam donnelly Aug 29 '17 at 16:44
  • You have your if block: `if (mail($to, $subject, $message_body)){` after the body of that, add an `else` statement, with the following: `print_r(error_get_last())`. In complete it would be something like `if (mail($to, $subject, $message_body)){ $success = "Message sent, thank you for contacting us!"; $name = $email = $phone = $message = $url = ''; }else{ print_r(error_get_last()); }` – Giedrius Aug 29 '17 at 16:49
  • Thanks added it. I received no errors on the page, should the errors be outputted into the xampp error logs file? – liam donnelly Aug 29 '17 at 16:52
  • No the `print_r` should display something. – Giedrius Aug 29 '17 at 17:08
  • $to = 'rudolf226@gmail.com'; $subject = 'Contact Form Submit'; if (mail($to, $subject, $message_body)){ $success = "Message sent, thank you for contacting us!"; $name = $email = $phone = $message = $url = ''; }else{ print_r(error_get_last()); } } } No error being displayed idk why – liam donnelly Aug 29 '17 at 17:27
  • Did you check spam folder to ensure you didn't get the message. Perhaps check if you can find some logs on the SMTP provider you're using. – Giedrius Aug 29 '17 at 17:38

0 Answers0