Ok i have a couple email functions, the one that works, sends an email to the recipiant and the one that doesnt work how i want it sends an email with an activation link in it.

the working email says its from (like i want it to say)

but the broken one says its from (not what i want)

but the functions are nearly identicle... what is the problem. what do you suggest i do to fix it? am i not seeing something that im doing wrong?

im going to cry foreal lol

thanks for the help guys


Just look for the email functions and ull see where im talking about.


this doesnt work like i want it to

function sendVerificationEmail($email, $code){
		$to = $email;
        $subject = "Please activate your account at ".SITE_NAME;
        $from = "registration@domain.com";
        
        $headers = "From: $from\n".
						   "Reply-To: support@domain.com\n".
						   "X-Mailer: PHP/".phpversion();
				
       
        $message = "Welcome to domain.com\n" .
            "We see that you recently opened an account with us.\n" .
            "Before you can start using our services you need to activate your account.\n" .
            "Follow the link below to activate your account.\n" . 
			SITE_URL."user/activate/?e=" . $to . "&c=" . $code . "&action=activate\n\n" .
            "_______________________\n\n" . "This is an automated response from ". SITE_NAME .
            "\n\n" . "Please do not respond to this email.";

        $send = mail($to, $subject, $message, $headers);
        if($send){
        	return true;
        }else{
        	return false;
        }
	}

this does work how i want it to... and its not differant

function sendEmail($to, $from, $subject, $message){
		if(empty($to) OR !preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])*(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*$/i', $to)){
			$this->m->addMessage("email-form", "error", "The recipients email must be in the form of email@domain.com.");		
		}
		if(empty($from) OR !preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])*(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*$/i', $from)){
			$this->m->addMessage("email-form", "error", "Your email must be the form of email@domain.com.");		
		}
		if(empty($subject)){
			$this->m->addMessage("email-form", "error", "You must include a subject.");
		}
		if(empty($message)){
			$this->m->addMessage("email-form", "error", "You must include a message.");
		}
		if($this->m->countMessages() > 0){
			//Stop
			return FALSE;
		}else{
			//Continue
			$save = mysql_query("/*query left empty on purpose (not my problem) */") or die(mysql_error());
			
			if($save){
				$headers = 'FROM: '.$from."\r\n".
						   'Reply-To: support@domain.com'."\r\n".
						   'X-Mailer: PHP/'.phpversion();
				$send = mail($to, $subject, $message, $headers);
				if($send){
					$this->m->addMessage("email-form", "success", "Your message has been sent.");
					return TRUE;
				}else{
					$this->m->addMessage("email-form", "error", "Your message could not be sent.");
					return FALSE;
				}
			}else{
				$this->m->addMessage("email-form", "error", "Your message could not be sent.");
				return FALSE;
			}
			
		}

Dani AI

Generated

Good troubleshooting by and the nudge from . The behavior you saw is common: the address you put in the message From: header is not the same thing as the SMTP envelope sender (the Return-Path). The PHP mail() call only writes headers into the message — the underlying MTA on the host decides the envelope sender, and many shared hosts will replace it with a local account (host@host299.hostmonster.com) if the address doesn’t exist or is not allowed.

Practical fixes to try:

  • Always build headers with CRLF line endings (\r\n) to avoid header-parsing issues on some MTAs.

  • Set the SMTP envelope sender via the mail() fifth parameter (if your host allows it). Example:

    mail($to, $subject, $message, $headers, '-fregistration@domain.com');
  • Create the registration@domain.com mailbox/alias on the server (what you did) or add it as an allowed sender in your control panel — hosts often rewrite to a local address otherwise.

  • For reliable, authenticated sending (and to avoid host rewriting entirely), use an SMTP library (PHPMailer or similar) with SMTP auth and proper credentials. Example minimal flow with PHPMailer: set SMTP host, enable SMTPAuth, call setFrom(...), and send().

Further troubleshooting:

  • Inspect the received message headers (look for Return-Path and Received) to see what was rewritten.
  • Check phpinfo() for sendmail_path and review your host’s mail logs or support docs.
  • Use SPF/DKIM records and authenticated SMTP to improve deliverability.

References: PHP mail() manual (php.net/manual/en/function.mail.php) and PHPMailer (github.com/PHPMailer/PHPMailer). If none of the above works, contact the host — they can explain their mail policy or allow an envelope-sender flag.

Recommended Answers

All 2 Replies

The all famous classic mail bug. Try the following:

function sendVerificationEmail($email, $code){
		$to = $email;
        $subject = "Please activate your account at ".SITE_NAME;
        $from = "registration@domain.com";
        
        $headers = "From: $from\n".
						   "Reply-To: support@domain.com\n".
						   "X-Mailer: PHP/".phpversion();
				
       
        $message = "Welcome to domain.com\n" .
            "We see that you recently opened an account with us.\n" .
            "Before you can start using our services you need to activate your account.\n" .
            "Follow the link below to activate your account.\n" . 
			SITE_URL."user/activate/?e=" . $to . "&c=" . $code . "&action=activate\n\n" .
            "_______________________\n\n" . "This is an automated response from ". SITE_NAME .
            "\n\n" . "Please do not respond to this email.";

        if(mail($to, $subject, $message, $headers)){ //bug fix
        	return true;
        }else{
        	return false;
        }
	}

If that doesn't work then in addition to that you might need to change the mail() inputs.

Miraculously, i seem to have fixed the bug myself...of of a couple of speculations. I ran a few tests, first i sent an email normaly with an email that does not exist, and it sent with the .

but then i actually created the email that i used before, but didnt exist. so i ran the function with the same exact vars/emails n all and it sent exactly how it was i have been trying to get it to send.

I also tried this test with the same procedures and it worked the same ways.

so my conclusion is, that the mail function pings my server to see if the "from" email actually exists. and if it exists, then it will use the inputed email. and if the email does not exist, the mail function will use the hosts defualt gateway.

thanks for your response!

i hope i helped somone else out along the way.

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.