2

I have a php script that sends an email from an HTML form. The issue is that the sender shows as CGI-Mailer in my inbox.

enter image description here

How can I set the sender address to be that of the sender and not CGI-Mailer?

<?php session_start();
if(isset($_POST['Submit'])) {
$youremail = 'info@complexny.com';
$fromsubject = $_POST['fname'];
$subject = $_POST['fname'];
$fname = $_POST['fname'];
$url = $_POST['url'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$headers  = "From: $mail \n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$message = $_POST['message'];
    $to = $youremail;
    $subject = ''.$fromsubject. ' is interested in a project with you.';
    $body = '
     Client: '.$fname.'
     Phone Number: '.$phone.'
     URL: '.$url.'
     E-mail: '.$mail.'

     Message:
     '.$message.'
     ';
echo "<p style='text-align:center'>Thank you for your feedback. We will be in contact shortly.<br/>Continue to <a href='/'>The Company/a></p>";
                                mail($to, $subject, $body);
 } else {
echo "You must write a message. </br> Please go to <a href='/contact.php'>Contact Page</a>";
}
?>
Matt
  • 1,561
  • 5
  • 26
  • 61

2 Answers2

2

You are not passing the additional_headers parameter to the mail function. Change the line with the call to mail to:

mail($to, $subject, $body, $headers);
  • Well, that's closer. Except now it shows `(unknown sender)` instead of the users name. – Matt May 31 '17 at 23:34
  • Try creating an array of headers rather than concatenating strings, that's what PHP's mail function expects. –  Jun 01 '17 at 09:49
0

I would suggest using PHPMailer rather than mail(). You can see how to do what you're after in the answer to this question: Setting replyTo field in email

Quoting from that question:

$this->phpmailer->AddReplyTo($replyEmail,$fromName);  //this is email2@example.com
$this->phpmailer->SetFrom($fromEmail, $fromName); //this is email1@example.com

You can find more info on PHPMailer here: https://github.com/PHPMailer/PHPMailer

David Findlay
  • 1,296
  • 1
  • 14
  • 30