- This topic is empty.
-
AuthorPosts
-
March 31, 2014 at 7:04 am #167193romullusParticipant
0 down vote favorite
I managed to make my contact form work but somehow i cant make it send the message structured how i want…
My code is this:
<?php $name = $_POST['name']; $email = $_POST['email']; $subject = $_POST['subject']; $text = $_POST['text']; $formcontent=' From: $name \n E-mail: $email \n Subject: $subject \n Text: $text'; $to = "[email protected]"; $subject = 'Message from a site visitor ' . $name; $message .= 'From: ' . $name . "\n"; $message .= 'E-mail: ' . $email . "\n"; $message .= 'Subject: ' . $subject . "\n"; $message .= 'Text: ' . $text; $headers = 'From: ' . $email . "\r\n"; $headers .= 'Reply-To: ' . $email . "\r\n"; $send_contact = mail($to, $subject, $email, $text); if($send_contact) { echo "Thank you!"; } else { echo "ERROR"; } ?>
I receive a mail having the subject that i want but doesn’t write the sender’s e-mail, or in the body of mail it doesn’t arrange the message by Subject, E-mail and Message…
Please help…
btw im a begginer in this.March 31, 2014 at 12:07 pm #167243__Participant$text = $_POST['text']; // ... $send_contact = mail($to, $subject, $email, $text);
Never do this. The fourth parameter of the mail function (where you are putting
$text
) is the headers parameter. By putting user-submitted text there, you are going to cause lots of errors (BEST case), or allow someone to use your contact form as their own personal spam service (likely).In general, Never Trust User Input. Always assume that it is wrong and/or an attack. You should never use anything that the user gives you until you have 1) checked it to make sure it is what you expected, and 2) made sure that any special characters are escaped so it has no unintended effects.
1) is called validation. 2) is called sanitization.
doesn’t write the sender’s e-mail
Are you sure the sender submitted an email? and that you’re using the correct form field names?
it doesn’t arrange the message by Subject, E-mail and Message…
Your code is building the message in the order “from”, “email”, “subject”, “text”.
Here’s some notes. Please look it over and post again with any questions.
March 31, 2014 at 12:32 pm #167245romullusParticipantwow…
i have no words to describe how much i appreciate your effort and that you made such a “for-dummies” explanation:) perfect for me…
i just must tell you i have no php.ini file… :|
March 31, 2014 at 1:10 pm #167246__ParticipantNo problem; I’m happy to help.
i just must tell you i have no php.ini file… :|
Well, yes, you do; though your host might not make it accessible to you. It would probably be worth your while to ask. But if they won’t let you touch it, you can set some options at runtime:
<?php error_reporting( -1 ); ini_set( 'display_errors',1 );
This is less effective (you still won’t see any parse errors, for example, just a blank page), but it’s better than nothing.
Let me know if you have any questions.
March 31, 2014 at 1:31 pm #167248romullusParticipantok:)
i made the changes. but i have no idea why ONLY if i put away $headers from mail() i receive the email. If $headers is inside the brackets than i dont get it…
what could be the problem?
March 31, 2014 at 1:55 pm #167250__ParticipantCan you show the actual code you are using to define
$headers
?You can use
var_dump
to see the value you actually end up with and make sure it is correct, before you send the email:print "<pre>"; var_dump( $headers );
March 31, 2014 at 1:58 pm #167251romullusParticipant<?php error_reporting(-1); ini_set('display_errors',1); $name = empty( $_POST['name'] )? null: $_POST["name"]; $email = empty( $_POST['email'] ) || ! filter_var( $_POST["email"],FILTER_VALIDATE_EMAIL )? null: $_POST["email"]; $subject = empty( $_POST['subject'] )? null: $_POST['subject']; $text = empty( $_POST['text'] )? null: $_POST['text']; if( $name === null || $email === null || $subject === null || $text === null ){ // error! print "Error!"; exit; } $to = "[email protected]"; $subject = 'Message from a site visitor ' .$name; $message = "From: $name E-mail: $email Subject: $subject Message: $text"; $headers = "From: $email\r\n"; $headers .= "Reply-to: $email\r\n"; $headers .= "Sender: [email protected]"; $send_contact = mail($to, $subject, $message ,$headers); if($send_contact){ echo "Thank you!"; } else{ echo "ERROR"; }
with $headers i dont get the mail. without it yes…
i will try now var_dump
March 31, 2014 at 2:02 pm #167253romullusParticipanti put var_dump how you said and i get this:
string(84) “From: [email protected]
Reply-to: [email protected]
Sender: [email protected]
Thank you!March 31, 2014 at 2:16 pm #167254__ParticipantI assume “[email protected]” is your actual email address. Does the domain name (myhosting.net) match the domain name of your website? Ask your host what domain name the mail is actually sent from. (When you leave the
$headers
out, whatFrom
address shows up on the email?)(To clarify, this doesn’t need to be a real email address. It can be a “noreply” address. It’s more important that it matches the domain name of the mail server that sends your mail. Usually, using your website’s domain name works just fine.)
You could also try putting your email address in the
From
header, and leaving theSender
header off.March 31, 2014 at 2:36 pm #167255romullusParticipantYes, the Sender address i copied from the Sender of the mail it has the domain of my hosting…
Do you think it might interpret wrong the variables?
Because i get this in my mail:
From: hajaha
E-mail: [email protected]
Subject: Message from a site visitor hajaha
Message: Ajjajajaffdd- and as i see the Subject that should be the one submited by the user in the contact form is the one that is also found in the subject of the email received.
So i get the e mail From: (and here its an email address with my domain name in it) and the Subject Message from a site visitor hajaha.
hmmm, did i understand smth wrong?
March 31, 2014 at 5:04 pm #167263__ParticipantDo you think it might interpret wrong the variables?
No.
did i understand smth wrong?
Don’t think so. But this sort of thing is really difficult to troubleshoot over a discussion thread. There are quite a few places where things might (emphasize might) be going wrong. I’m not really sure what else to suggest.
What email client do you use (thunderbird, outlook, gmail, etc.)? Do you know how to view the raw email headers?
April 1, 2014 at 12:09 am #167272romullusParticipanti am using gmail…
no unfortunately i dont know how to view raw headers…
April 1, 2014 at 12:24 am #167276__Participantviewing email headers in gmail:
- Log in to Gmail
- Open the message you’d like to view headers for.
- Click the down arrow next to Reply, at the top of the message pane.
- Select Show Original. The full headers will appear in a new window.
post back here (you can ******** any personal/sensitive information).
April 1, 2014 at 1:05 am #167279romullusParticipantthis is the raw email that i get…
Delivered-To: *******@gmail.com
Received: by 10.170.205.** with SMTP id w63csp196707yke;
Tue, 1 Apr 2014 00:51:24 -0700 (PDT)
X-Received: by 10.180.92.*** with SMTP id co4mr17983882wib.50.1396338684019;
Tue, 01 Apr 2014 00:51:24 -0700 (PDT)
Return-Path: <****@ls14..net>
Received: from ls14.globe.net (ls14.globe.net. [94.177.99.])
by mx.google.com with ESMTPS id 45si26785910eeh.33.2014.04.01.00.51.23
for <*****@gmail.com>
(version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
Tue, 01 Apr 2014 00:51:23 -0700 (PDT)
Received-SPF: pass (google.com: best guess record for domain of ****@ls14.globe.net designates 94.177.99. as permitted sender) client-ip=94.177.99.***;
Authentication-Results: mx.google.com;
spf=pass (google.com: best guess record for domain of ****@ls14.globe.net designates 94.177.99. as permitted sender) smtp.mail=****@ls14.globe.net
Received: from **** by ls14.globe.net with local (Exim 4.82)
(envelope-from <****@ls14.globe.net>)
id 1WUtTe-001EOF-3c
for ********@gmail.com; Tue, 01 Apr 2014 10:51:22 +0300
To: *******@gmail.com
Subject: Message from a site visitor name
X-PHP-Script: *******.com/mobile/mail.php for 77.180.146.244
Message-Id: <[email protected].net>
From: ****@ls14.globe.net
Date: Tue, 01 Apr 2014 10:51:22 +0300
X-AntiAbuse: This header was added to track abuse, please include it with any abuse report
X-AntiAbuse: Primary Hostname – ls14.globe.net
X-AntiAbuse: Original Domain – gmail.com
X-AntiAbuse: Originator/Caller UID/GID – [620 500] / [47 12]
X-AntiAbuse: Sender Address Domain – ls14.globe.net
X-Get-Message-Sender-Via: ls14.globe.net: authenticated_id: theballr/primary_hostname/system user
X-Source:
X-Source-Args: /usr/sbin/proxyexec -q -d -s /var/run/proxyexec/cagefs.sock/socket /bin/cagefs.server
X-Source-Dir: ********.com:/public_html/mobileFrom: name
E-mail: [email protected]
Subject: Message from a site visitor name
Message: Messagefgfggggg***i checked the spam folder and when i have $headers in the mail() function it sends the mail there. but at the senders name it writest (unknown sender)
and the message that i get in the mail is like this:
From: [email protected]
Reply-to: [email protected]
Sender: ***@ls14.globe***.netApril 1, 2014 at 11:59 am #167345__ParticipantAll those
X-AntiAbuse
headers mean that various MTA’s along the delivery path thought the message was suspicious, for one reason or another. I would contact your web host and ask what headers+values they would suggest sending for best results. Who knows if it would help, though—some hosts simply don’t have a good mailing reputation, and aren’t motivated to maintain one.Only other suggestion I might make would be to try putting your domain’s email address in the
From
header, and just leaving the user’s email in theReply-to
header. But that’s just a guess… sorry. -
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.