0

I have a (php) contact form on a webpage that's hosted through IIS 8.0 and I would like for the form to be sent to an email when it's submitted. There is already an SMTP server setup on a different server on the network, so I'm trying to use that, but the php mail() function is failing and I'm not sure why. 

I've configured SMTP E-Mail in IIS to a valid email address, to the IP of the SMTP server, and utilizing the correct port. 

Here's my PHP, although I don't think the issue is with my PHP. 

<?php
$firstName = $_POST['name'];
$lastName = $_POST['name'];
$email = $_POST['email'];
$companyName= $_POST['name'];   
$jobTitle = $_POST['name'];
$phoneNumber = $_POST['number'];
$comments = $_POST['comments'];

$header = "From: admin@xxxxx.com";
$to = 'xxxxx@gmail.com';
$subject = 'Demo request';

$message = "From: $firstName, $lastName\r\n 
    E-mail: $email\r\n 
    Company Name: $companyName\r\n 
    Job Title: $jobTitle\r\n
    Phone Number: $phoneNumber\r\n
    Comments: $comments";


if($_POST['submit']) {
    if(mail ($to, $subject, $message, $header)) {
        echo('Your message has been sent!');
    } else {
        echo('Something went wrong, please try again');
    }
}
?>
djd97
  • 77
  • 1
  • 1
  • 9
  • Do you have your php settings configured to use the remote server? If not, start there... Search your php.ini for specific mail win(32) settings. – Raphioly-San Jun 27 '16 at 18:14
  • Oh, SMTP should probably be the IP of the server, not localhost. [mail function] ; For Win32 only. ; http://php.net/smtp SMTP = localhost ; http://php.net/smtp-port smtp_port = 25 – djd97 Jun 27 '16 at 18:16
  • yup. Another option (personal preference) is to use a Mail library (e.g. phpmailer). Before sending your mail, you can set the SMTP servers' settings, even use TLS... But basic mail() should suffice for simple mailings... – Raphioly-San Jun 27 '16 at 18:19
  • Try using [PHPMailer](https://github.com/PHPMailer/PHPMailer). Enable debugging and you should be able to diagnose the problems – Machavity Jun 27 '16 at 18:22
  • Thanks, it's working now, I just completely forgot about the mail settings in the .ini file. – djd97 Jun 27 '16 at 18:36
  • When my php contact form is submitted the webpage redirects to the .php file. I want to end up back on the .html page, and I'd rather not use AJAX. I can get it to end up back where I want it, but I can't also make it display a 'success message'.How could I go about doing that -- here's what I currently have (which ends up back on the html page without ever displaying an alert. if(mail (....)) { header('Location: page.htm'); echo ''; } exit(); – djd97 Jun 27 '16 at 19:16
  • That's a question unrelated to this issue, but here's my 50 cent: One thing you got right, is that you don't want that during your post. Try setting a simple message variable in your `$_SESSION` superglobal, read it the first time a page pops up, display the message and clear its' value. – Raphioly-San Jun 28 '16 at 05:52
  • I tried to do that, but something isn't working properly. http://stackoverflow.com/questions/38078703/using-php-session-to-display-status-messages?noredirect=1#comment63594016_38078703 – djd97 Jun 28 '16 at 14:29

0 Answers0