0

Using a Bootstrap mail form. Form delivers mail, except in the from variable it displays the server name instead of the sender's email address.

Could someone explain how I get the $name variable to display as "from" OR at the very least, display the name of the domain the email was sent from rather then the server name?

 <?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $business = $_POST['business'];
        $phone = $_POST['phone'];
        $message = $_POST['message'];
        $from = 'From website of xxxxxx'; 
        $to = 'user@somedomainname.com'; 
        $subject = 'New Message ';

        $body = "From: $name\n E-Mail: $email\n Business Name: $business\n  Phone: $phone\nMessage: $message";

        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

         // Check if business has been entered
        if (!$_POST['business']) {
            $errBusiness = 'Please enter your business or organization name';
        }

         // Check if phone has been entered
        if (!$_POST['phone']) {
            $errPhone = 'Please enter your phone number';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch.</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
    }
}
    }
?>
ControlZ
  • 171
  • 2
  • 14
  • The `from` field is to be an email address, like noreply@yourdomain. Try if that already does the trick. – Tobi Nary Jan 13 '16 at 08:06
  • I tried that prior to posting and it does not work Jan. Email (from:) is still showing the server name where the site is hosted. – ControlZ Jan 13 '16 at 08:08

2 Answers2

0

The problem may reside in your call to the mail function.

As the docs say,

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

and, more interestingly,

additional_headers (optional)

String to be inserted at the end of the email header.

This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n). If outside data are used to compose this header, the data should be sanitized so that no unwanted headers could be injected.

Note:

additional_headers does not have mail header injection protection. Therefore, users must make sure specified headers are safe and contains headers only. i.e. Never start mail body by putting multiple newlines.

Note:

When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.

Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. The From header sets also Return-Path under Windows.

Note:

If messages are not received, try using a LF (\n) only. Some Unix mail transfer agents (most notably » qmail) replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822.

Community
  • 1
  • 1
Tobi Nary
  • 4,387
  • 4
  • 26
  • 48
  • I get the optional additional headers, however I am not sure I understand "mail function." as the PHP I posted is all there is besides a Bootstrap validation script. Here's what I get when the form is posted: domainname@servername.com – ControlZ Jan 13 '16 at 08:22
  • `additional headers` is inserted as raw in the header parts. So `$from` should be something like `From: noreply@yourdomain.tld`. – Tobi Nary Jan 13 '16 at 08:29
0

Turns out all I needed to was to add a line to the php.ini file and drop in root directory of server. Did not have to add $headers to existing script of mod the from: line.

Some would prefer the from: line to display the actual sender's address, but I've found this creates havoc with spam filters as each form submitted is from a different user. I am sure there is a way to correct this, just now 100% sure how to do so,

sendmail_path="/usr/sbin/sendmail -t -i -f webmaster@somedomainname.com"
ControlZ
  • 171
  • 2
  • 14