0

Ok so I set up a php mailer on 2 separate pages on a site I am working on. I had previously worked through everything in the mailer on a site I built and it was straight forward how to set the email after setting all the variables:

mail("$email",
     "Receipt: $thesubject",
     "$message",
     "From: $replyemail\nReply-To: $replyemail");

So this was what was at the end of the contact (processing) page. I have $email and $replyemail being put into the $message and they come out correctly in the message that gets sent to my email specified by $email.

The part I don't really understand is the address the message says it is coming from is not that $replyemail but instead it says it is being sent from:

rtl.srv@gmail.com

I saw a few posts that were similar but none of them fixed the issue, I followed this and checked to make sure the servers php.ini files had SAFE_MODE = off. Then added

'-f $replyemail'

to the end of the mail function above like that link advised but it didn't change anything... I saw somewhere else that it explained you may have to add the email address to the file /etc/mail/trusted-users but the issue is that email is dynamic. Since that email name is whatever the user input as their email. Then the email is sent to the site admin to review, and I want the email to say it is from the users email that filled out the form.

I know the variables are set correctly because they are being printed out correctly inside the message of the email. So if anyone has any idea why this is happening or how I could go about fixing it I would appreciate any insight.

The project is built in wordpress but I just dropped the files onto the server through SSH, not sure if that has anything to do with it.

Thanks,

-Alan

Community
  • 1
  • 1
Alan DeLonga
  • 454
  • 1
  • 10
  • 27

2 Answers2

0

There are a number of issues that can cause this, and I encountered this issue a while ago when I built my site. I solved it by first creating a function to handle the mailout for me:

function mailouthtml($to, $title, $body, $from){
    if(!isset($from)){
        $from = 'Default Sender<address@example.com>';
    }
    $header .= "Reply-To: $from\r\n"; 
    $header .= "Return-Path: $from\r\n"; 
    $header .= "From: $from\r\n"; 
    $header .= "Organization: Example.com, Inc.\r\n"; 
    $header .= "Content-Type: text/html\r\n"; 
    mail($to,$title,$body,$header,"-f $from");
}

Let me break this down a bit. The function calls for a To address, the email title, the email body, and the From address. The From address is used to add information to the email header. The header information of your email is what tells your mail server what to do with your email, and it needs a bit of info in order to handle the message properly.

The mail() function in PHP is formatted as follows:

mail($to,$title,$body,$headers,$additional_parameters);

The mailouthtml() function I've created here constructs the header manually, and adds the additional parameter "-f $from" to set the "From" field explicitly. The $from variable is optional in the function call; if it isn't present, it sets a default. You can find more information on the mail function Here.

Note the following:

  • Most MTA's require a Reply-To, Return-Path and From field, or it may be flagged as spam.
  • Content-Type is required tell what type of content the mail server is delivering, typically text/html or text/plain.
  • I have an if(){} statement that sets a default address if none is specified. You can omit 'Default Sender' if you would rather not specify a name. You can also change that to whatever you'd like.
  • Organization is optional.

I would also recommend you have SPF enabled on your domain name, and DKIM signing on your messages. These are both implemented via DNS entries for your domain name. If you don't have access to that, no big deal; they're meant to control spam.

Also, if you have a dedicated server and your own IP address, make sure you have a Reverse DNS record set up. This will also help foreign MTA's identify your message as authentic.

Adam
  • 33
  • 4
0

don't make this to yourself. Use some mature library like phpmailer or swiftmailer, they will help you to avoid these kind of troubles...I know there are more includes, etc, but there is not point to give fight to this. If you still want to do this, try setting the header Returh-path

Gabriel Sosa
  • 7,897
  • 4
  • 38
  • 48