PHP Mail function error

Discussion in 'Site Programming, Development and Design' started by gabriel93, Dec 9, 2009.

  1. Hey,

    I have a php form on my website. When i tested it it gave me the error shown in the screenshot,[​IMG]

    the code for the mail function is: mail($your_email, "Contact Form Sent", $body, "From: $email\r\nReply-To: $email\r\nContent-Type: text/plain; charset=ISO-8859-1\r\nMIME-Version: 1.0");

    Is there a way to fix it or should i just use asp.net ajax?
     
  2. Ray

    Ray

    What is the smtp server you are using?
     
  3. FredC

    FredC Winhost Staff

    looks like you are using an invalid mail server in your php code. if you still have problem, pls post the code.
     
  4. <?php

    $your_email = 'gabriel@gabrielsadaka.com.au'; // Email to send message to

    if ($_SERVER['REQUEST_METHOD'] != 'POST') exit; // Quit if it is not a form post

    // quick way clean up incoming fields
    foreach($_POST as $key => $value) $_POST[$key] = urldecode(trim($value));

    // get form data into shorter variables
    // each $_POST variable is named based on the form field's id value
    $name = $_POST['sender_name'];
    $email = $_POST['sender_email'];
    $phone = $_POST['sender_phone'];
    $address = $_POST['sender_address'];
    $message = $_POST['message'];
    $code = $_POST['code'];

    $errors = array(); // array of errors

    // basic validation
    if ($name == '') {
    $errors[] = "Please enter your name";
    }

    if ($email == '') {
    $errors[] = "Please enter your email address";
    } else if (strpos($email, '@') === false) {
    $errors[] = "Please enter a valid email address";
    }

    if ($phone == '') {
    $errors[] = "Please enter your phone number";
    }

    if ($message == '') {
    $errors[] = "Please enter a message to send";
    }


    if (sizeof($errors) == 0) {
    // only check the code if there are no other errors
    require_once 'securimage/securimage.php';
    $img = new Securimage;
    if ($img->check($code) == false) {
    $errors[] = "Incorrect security code entered";
    } // if the code checked is correct, it is destroyed to prevent re-use
    }

    if (sizeof($errors) > 0) {
    // if errors, send the error message
    $str = implode("\n", $errors);
    die("There was an error with your submission! Please correct the following:\n\n" . $str);
    }

    $time = date('r');
    $body = <<<EOD
    Hi!

    A message was sent to you from $name on $time.
    Their phone number is: $phone
    Here is their message:

    $message
    EOD;

    // send email
    mail($your_email, "Contact Form Sent", $body, "From: $email\r\nReply-To: $email\r\nContent-Type: text/plain; charset=ISO-8859-1\r\nMIME-Version: 1.0");


    die('OK'); // send success indicator

    ?>

    This script worked fine with my other host and i can send emails and recieve emails perfectly using this email address with Winhost so i don't understand what the issue is.
     
    Last edited by a moderator: Oct 14, 2015
  5. Ray

    Ray

    This code doesn't show us what smtp server you are using. I suspect you are using 'localhost'. We do not use 'localhost'. You will need to use your mail server we provided you with the account. Keep in mind that you need to past smtp authentication. That is the full email address and password of the email account on our email system.
     
  6. php mail() looks for localhost by default. It's not very flexible. The only way I know of to specify an SMTP host other than localhost is to modify the php.ini file and unfortunately that can't be done on a shared Windows server.

    I wrote a php function to send mail some time ago using fsockopen that allowed you to specify an SMTP server, but I never had to pass authentication to the server, so that function wouldn't work here. It's probably do-able though, if you want to write something from scratch (hmm, I guess that also assumes that we have complied php with sockets, and I'm not sure if that's the case - anyone else know?).

    If not, you may just want to dig around and see if you can find a function that will pass authentication to a specific SMTP server.

    If you find something, let us know!
     
  7. lol thank you for all your help, forget about php then it's alot easier with asp.net that way you can specify everything.
     
  8. i tried making a form using asp.net, i got it all working fine but when i tried to add a script manager and an update panel for ASP.NET AJAX from the toolkit it doesn't work is there any includes that i have to do or do i have to copy the dll files for the toolkit in my app_data folder or something like that?
     
  9. What do you mean 'it doesn't work' ?

    I've used the updatepanel quite a bit, so I can probably help you with this. A common problem with the UpdatePanel, though, is that it won't throw errors. So if it seems to not be updating anything at all, remove whatever is inside the update panel from it and try it again.

    If it throws an error, address it.
     
  10. it can't find it

    was i supposed to include anything in my web.config file for it to work properly?
     
  11. Ray

    Ray

    gabriel93 - you're still not telling us what doesn't work. We will need more details if we are going to help you. It will be helpful if you give us the error message.
     
  12. Sorry for the trouble,

    I moved back to php because i realised that even if i did get the ASP.NET form working that would mean i would have to change the extension for the file which would effect my search engine rating.

    However the only i could find that i could get SMTP authentication working in PHP is by using the PEAR Mail Class but according to the error i am getting, this class is not installed on the server.

    Warning: require_once(Mail.php) function.require-once failed to open stream: no such file or directory in E:\web\sadakasr\sadakasrendering\process.php on line 2

    Fatal error: require_once() function.require: Failed opening required 'Mail.php' (include_path='.;C:\php5\pear') in E:\web\sadakasr\sadakasrendering\process.php on line 2

    This is the php code:
    <?php
    require_once(Mail.php');
    $your_email = 'gabriel@gabrielsadaka.com.au'; // Email to send message to
    $host = "mail.sadakasrendering.com.au";
    $username = "postmaster@sadakasrendering.com.au";
    $password = "losers512";
    $subject = "Contact Form Sent";

    if ($_SERVER['REQUEST_METHOD'] != 'POST') exit; // Quit if it is not a form post

    // quick way clean up incoming fields
    foreach($_POST as $key => $value) $_POST[$key] = urldecode(trim($value));

    // get form data into shorter variables
    // each $_POST variable is named based on the form field's id value
    $name = $_POST['sender_name'];
    $email = $_POST['sender_email'];
    $phone = $_POST['sender_phone'];
    $address = $_POST['sender_address'];
    $message = $_POST['message'];
    $code = $_POST['code'];

    $errors = array(); // array of errors

    // basic validation
    if ($name == '') {
    $errors[] = "Please enter your name";
    }

    if ($email == '') {
    $errors[] = "Please enter your email address";
    } else if (strpos($email, '@') === false) {
    $errors[] = "Please enter a valid email address";
    }

    if ($phone == '') {
    $errors[] = "Please enter your phone number";
    }

    if ($message == '') {
    $errors[] = "Please enter a message to send";
    }


    if (sizeof($errors) == 0) {
    // only check the code if there are no other errors
    require_once 'securimage/securimage.php';
    $img = new Securimage;
    if ($img->check($code) == false) {
    $errors[] = "Incorrect security code entered";
    } // if the code checked is correct, it is destroyed to prevent re-use
    }

    if (sizeof($errors) > 0) {
    // if errors, send the error message
    $str = implode("\n", $errors);
    die("There was an error with your submission! Please correct the following:\n\n" . $str);
    }

    $time = date('r');
    $body = <<<EOD
    Hi!

    A message was sent to you from $name on $time.
    Their phone number is: $phone
    Here is their message:

    $message
    EOD;

    // send email

    $headers = array ('From' => $your_email,
    'To' => $email,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));

    $mail = $smtp->send($email, $headers, $body);

    if (PEAR::isError($mail)) {
    echo("<p>" . $mail->getMessage() . "</p>");
    } else {
    echo("<p>Message successfully sent!</p>");
    }



    die('OK'); // send success indicator

    ?>
     
  13. How would the file ending effect your search engine ranking?
     
  14. I'm not sure why it happens but with my last host i had all the files as .php but once i changed them all .html. My website ranking dropped to the bottom and it took a few months before it went back up to the same position it was at.

    I don't understand why it happens, it only happened with google though, both Yahoo and MSN(Now Bing) maintained my ranking.

    Besides I am more confident with PHP and Jquery as opposed to ASP.NET AJAX.
     
  15. Ray

    Ray

    It could be that they are caching your website with the old file extension .php. You may want to check Google's website and verify how they rank sites. Google tends to be fairly strict with search engine rankings.
     
  16. Yes but even then i would still prefer to use PHP, does anyone know a good method of using SMTP authentication in PHP, if so could you please provide me with an example of some sort.
     
  17. Ray

    Ray

    The problem is that you will need to use the PEAR method if you are going to use SMTP authentication in your application. And unfortunately PEAR is not installed on the servers.
     
  18. Yeh i tried the PEAR method and i kept running into errors, that would explain a lot. So is there no other way to get it working?
     
  19. Not with php - nothing that I've been able to find anyway.
     
  20. Ok thank you for all your help, i will just use asp.net ajax then it shouldn't be too hard to get working eventually :)
     

Share This Page