2

I have searched stack overflow as well as the Ubuntu forums. I am writing a website and I am hosting it myself using a LAMP server. I have a form that a user fills out and want it to send an email once the user clicks the submit button. So far I have tried sendmail, ssmtp, phpmailer, and installing mailutils and using postfix. The reason I believe that it's an issue with the mail program is because after testing I'm getting no errors from the code and I am unable to send mail from the commandline. I would prefer not to use phpmailer as I don't want to hardcode any passwords in the website in the event it gets compromised. I would appreciate any help someone could give me. I have included the mail script if you need config files let me know Thank you in advance.

<?php 
session_start();

if(isset($_POST['submit'])){
    $to = "jmaggsdf@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $subject = "Website Email";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];

    $type = 'plain'; // or HTML
    $charset = 'utf-8';

    $_SESSION['from'] = $_POST['email'];
    $_SESSION['first_name'] = $_POST['first_name'];
    $_SESSION['last_name'] = $_POST['last_name'];
    $_SESSION['message'] = $_POST['message'];

    $headers    = 'MIME-Version: 1.0' . "\r\n";
    $headers    .= 'Content-type: text/plain; charset=utf-8' . "\r\n";
    $headers    .= 'To: ' . $to . '<' . $to . '>' . "\r\n";
    $headers    .= 'From: ' . $from . '<' . $from . '>' . "\r\n";

    if($first_name == NULL || $last_name == NULL || $from == NULL || $message == NULL){
        header("Location: mailError.php");
        die();
    }
    else{
        //$headers2 = "From:" . $to;
        mail($to,$subject,$message,$headers);
        header("Location: thanksPage.php");
        //mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
        //echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    }
}
?>
JMaggsDF
  • 21
  • 3
  • Does your server have a mail server configured. Remember `mail()` only passes the mail to a mail server, it does not actually send it itself – RiggsFolly Feb 01 '17 at 03:42
  • Have you tried Google Mail API for PHP ? It's vert convenient and fast. Here's the link to learn more: https://cloud.google.com/appengine/docs/php/mail/ – Rishi Feb 01 '17 at 03:45
  • 1
    Or there is the almost ubiquitus `phpMailer` https://github.com/PHPMailer/PHPMailer Then you dont need to install and maintain a mail server – RiggsFolly Feb 01 '17 at 03:46
  • Try phpmailer...... – Vikas Umrao Feb 01 '17 at 03:49
  • @RiggsFolly I do know that and have read about that in my research I tried a few different programs but still no dice and no errors. Am I missing something in the config? Also if I use phpmailer for this purpose will I have to hardcode any passwords? – JMaggsDF Feb 01 '17 at 03:56

1 Answers1

0

It you are sending mail using local host then it will not send mail. It requires live server.

Tosif
  • 36
  • 5
  • I realize that but will just simply installing sendmail, or postfix do the trick or do I need to alter the php.ini or the mail config files on the server? – JMaggsDF Feb 01 '17 at 04:09
  • mail() will work, just refer some examples or you can use third partty softwares like phpmailer. – Tosif Feb 01 '17 at 04:18