Can anyone help me with the mail function? The mail is sending with the code below:

//  Set up Email Parameters
    $from = "Condolence Submission";
    $subject = "Condolence";

// To send HTML mail, set the Content-type header. */
    $headers = 'MIME-Version: 1.0' . "\r\n" .
			'Content-type: text/html; charset=iso-8859-1' .  \r\n" .
                       'To: Tara <tmv105@gmail.com>' . "\r\n" .
                       'From: $from' . "\r\n" .
                       'Cc: greatfultobe@yahoo.com' . "\r\n" .
                       'Bcc: talong@netscape.com' . "\r\n" .
                       'X-Mailer: PHP/' . phpversion();
 
//  Send Email
mail($to,$subject,$message,$headers);

...but the "From" in the email is showing as "$from" and not the value I have assigned the variable. I tried do this for the "From"

'From: Condolence Submission' . "\r\n" .

and I get (unknown sender) in the "From" of my email.

Dani AI

Generated

already found the immediate fix: mail readers and many MTAs expect a valid mailbox in the From header. Beyond that, a few practical points will prevent surprises and improve deliverability.

Use a site-owned address as the envelope/sender and put the visitor's address in Reply-To. This avoids SPF/DKIM failures when you try to send mail claiming to be from someone on another domain. Also format the header with a display name plus angle-bracketed email (for example, Site Name <no-reply@yourdomain.com>), and always validate and strip control characters from user input.

Example pattern (sanitize + safe headers):

$reply = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$reply = preg_replace('/[\r\n]+/', '', $reply);             // prevent CRLF injection
if (!filter_var($reply, FILTER_VALIDATE_EMAIL)) {
    $reply = null;
}
$headers  = "From: Site Notifications <no-reply@yourdomain.com>\r\n";
if ($reply) { $headers .= "Reply-To: {$reply}\r\n"; }
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($to, $subject, $message, $headers, '-fno-reply@yourdomain.com');

Sanitization and injection prevention are vital — never inject raw $_POST into headers. See OWASP on CRLF/HTTP header injection for risks and mitigation (). For reliable, feature-rich sending (auth, attachments, proper headers), use a maintained library such as PHPMailer (https://github.com/PHPMailer/PHPMailer) and consult the PHP mail() manual for the envelope parameter and platform notes (https://www.php.net/manual/en/function.mail.php).

I actually figured it out. If I place an email address in the from, it works. I suppose that PHP mail() requires an email address here. Here is what I did:

//  Set up Email Parameters
    $from = $_POST['email'];
    $subject = "Condolence";
	 
	// To send HTML mail, set the Content-type header. */
	$headers = 'MIME-Version: 1.0' . "\r\n" .
				'Content-type: text/html; charset=iso-8859-1' . "\r\n" .

	/* additional headers */
	//$headers .= "To: Michael <diehlfhi@aol.com>, Tara <tmv105@gmail.com>\r\n";
	'To: Tara <tmv105@gmail.com>' . "\r\n" .
	//'From: condolence@deihl.com' . "\r\n" .
	'From:'. $from . "\r\n" .
	'Cc: greatfultobe@yahoo.com' . "\r\n" .
	'Bcc: talong@netscape.com' . "\r\n" .
	'X-Mailer: PHP/' . phpversion();
     
    //  Send Email
    mail($to,$subject,$message,$headers);

Both ways work...either hard coding an email into the "From" or filling the value in from a variable set from data on my form.

Thought that I would give an attempt of an explanation to any who might wanna know. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.