• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

PHP form seems to be sending from local host?

Hello, I'm a complete newb at PHP. Infact, I know next to nothing. So I got this code online and I have the form working on my client's site and was testing it out with two of my email addresses, one as recipient and one as sender.

Well, I'm receiving the emails but the sender is always my clients email address on her server.

I tried calling her hosts support and they just ain't as good as you guys.

Hope you can help me with this one.
//The following code is from file process.php//
 
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ||
!strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
die("Bad referer");
$msg="Website Message:nnn";
foreach($_POST as $key => $val){
if (is_array($val)){
$msg.="Item: $keynn";
foreach($val as $v){
$v = stripslashes($v);
$msg.=" $vnn";
}
} else {
$val = stripslashes($val);
$msg.="$key: $valnn";
}
}
$recipient="laughhearty@gmail.com";
$subject="Mari Martin Fan";
error_reporting(0);
if (mail($recipient, $subject, $msg)){
header( "Location: http://www.marimartinmusic.com/thankyou.php" );
} else
echo "An error occurred and the message could not be sent.";
} else
echo "Bad request method";
?>
 
Read more: "Contact Form | PHP" - http://www.webdesign.org/web/web-programming/php/contact-form.9874.html#ixzz0CulmjQxo&A
 
 
//The following code is from contact.php//
 
	<form method="post" action="process.php">
  <fieldset>
  	<div class="contact">
    <label for="firstname">First Name:</label>
    <input type="text" name="firstname" id="firstname" />
    </div>
    <div class="contact">
    <label for="firstname">Last Name:</label>
    <input type="text" name="lastname" id="lastname" />
    </div>
    <div class="contact">
    <label for="email">E-mail:</label>
    <input type="text" name="email" id="email" />
    </div>
        <div class="contact">
    <label for="comments">Comments:</label>
    <textarea name="comments" id="comments" rows="6" cols="28"></textarea>
    </div>
 	<div class="send">
  	<button type="submit" name="submit" id="submit" class="submit_button">submit</button>
  	</div>
  	</fieldset>
  	</form>

Open in new window

0
Alicia St Rose
Asked:
Alicia St Rose
  • 4
  • 3
  • 2
  • +2
1 Solution
 
dwaynecharringtonCommented:
The problem is you haven't specified a certain header option for the mail command.  Try the following code attached. This code presumes that the email address you want it to appear from is the email address the end user has entered as their email address.

//The following code is from file process.php//
 
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ||
!strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
die("Bad referer");
$msg="Website Message:nnn";
foreach($_POST as $key => $val){
if (is_array($val)){
$msg.="Item: $keynn";
foreach($val as $v){
$v = stripslashes($v);
$msg.=" $vnn";
}
} else {
$val = stripslashes($val);
$msg.="$key: $valnn";
}
}
$recipient="laughhearty@gmail.com";
$subject="Mari Martin Fan";
$headers = "From: $_POST['email']";
error_reporting(0);
if (mail($recipient, $subject, $msg, $headers)){
header( "Location: http://www.marimartinmusic.com/thankyou.php" );
} else
echo "An error occurred and the message could not be sent.";
} else
echo "Bad request method";
?>
 
Read more: "Contact Form | PHP" - http://www.webdesign.org/web/web-programming/php/contact-form.9874.html#ixzz0CulmjQxo&A
 
 
//The following code is from contact.php//
 
        <form method="post" action="process.php">
  <fieldset>
        <div class="contact">
    <label for="firstname">First Name:</label>
    <input type="text" name="firstname" id="firstname" />
    </div>
    <div class="contact">
    <label for="firstname">Last Name:</label>
    <input type="text" name="lastname" id="lastname" />
    </div>
    <div class="contact">
    <label for="email">E-mail:</label>
    <input type="text" name="email" id="email" />
    </div>
        <div class="contact">
    <label for="comments">Comments:</label>
    <textarea name="comments" id="comments" rows="6" cols="28"></textarea>
    </div>
        <div class="send">
        <button type="submit" name="submit" id="submit" class="submit_button">submit</button>
        </div>
        </fieldset>
        </form>

Open in new window

0
 
Alicia St RoseOwner & Principle Developer/DesignerAuthor Commented:
Hmm... it's not working at all now.
0
 
palanee83Commented:
Hi,

You can use PHP copy() function to upload the the file from URL to your server.

Both source  and dest  can be URLs if the "fopen wrappers" on your server.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
palanee83Commented:
Extremely sorry.. wrongly posted (previous post)
0
 
shobinsunCommented:
Hello,

You can use :mail($recipient, $subject, $msg, 'From:yourmail@example.com')

Instead of : mail($recipient, $subject, $msg)

Then it will display the mail address what you want.

Hope this will help you.


Regards.
0
 
Alicia St RoseOwner & Principle Developer/DesignerAuthor Commented:
That isn't working either.
This is what I want: When someone fills in the form I want it to show that it's either coming from their email address or have that information in the email.
Right now it's showing that the email is coming from my clients server when I type my own email address in the form and the first name and last name are not showing up in the email even though I've submitted them in the form.
here's what I get in the email:

Website Message:nnnfirstname: lastname: email: comments: submit:

See, none of the information I put in the form...

A~

0
 
shobinsunCommented:
Hello,

You can get the values from the form  by using $_POST['firstname'] etc.

Then concatenate all to $msg.
0
 
Alicia St RoseOwner & Principle Developer/DesignerAuthor Commented:
I know next to nothing about PHP. Is this where the code should go?

A~


<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ||
!strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
die("Bad referer");
$msg="$_POST['firstname'] . $_POST['lastname'] . $_POST['email'] . $_POST['comments']";
foreach($_POST as $key => $val){
if (is_array($val)){
$msg.="Item: $keynn";
foreach($val as $v){
$v = stripslashes($v);
$msg.=" $vnn";
}
} else {
$val = stripslashes($val);
$msg.="$key: $valnn";
}
}
$recipient="laughhearty@gmail.com";
$subject="Mari Martin Fan";
error_reporting(0);
if (mail($recipient, $subject, $msg)){
header( "Location: http://www.marimartinmusic.com/thankyou.php" );
} else
echo "An error occurred and the message could not be sent.";
} else
echo "Bad request method";
?>

Open in new window

0
 
shobinsunCommented:
Hello

 use the following code:

<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ||
!strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
die("Bad referer");
$msg='Message:nnn';
$msg.="Firstname: ".$_POST['firstname'] ."Lastname: ". $_POST['lastname'] ."Email: ". $_POST['email'] ."Coments: ". $_POST['comments'];
$recipient='laughhearty@gmail.com';
$subject='Mari Martin Fan';
error_reporting(0);
if (mail($recipient, $subject, $msg)){
header( "Location: http://localhost/thankyou.php" );
} else
echo "An error occurred and the message could not be sent.";
} else
echo "Bad request method";
?>

Open in new window

0
 
Chris GralikeSpecialistCommented:
Hi,

You have little to no knowledge about PHP, so ill refference to manuals and hope you read your way to the sollution with some hints from my part ;-)

First the "From" field in the mail header can be controlled in more then one location. Firstly the "Default" from is determined in the "PHP.ini" file. You need to find the mail directive to alter it (if you have access to the PHP.ini file that is)

Have a look at the following manual for more info on the subject ;-)
http://nl3.php.net/manual/en/mail.setup.php
Another method is by adding so called "header" to the mail command. The header you are looking for is called the mail from:[addr@domain.ext] \r\n. Where the php.ini will make sure that the default email is correct (no headers in the mail command) the headers in the mail() function will allow you to use a scripting approach and allow you to conditionally alter / modify the from header.

Have a look in the following manual for more info on the PHP mail command.
http://nl3.php.net/manual/en/function.mail.php
Next to that, altering the headers might require one to know what headers can be modified, have a look here for more info.
http://www.expita.com/header1.html
In the end the header part could look somewhat like the attached code.



<?php
/* Alter the php.ini from the code (runtime change) to make them perm. you need to alter the php.ini itself and restart the webservice */
ini_set("SMTP","smtp.example.com" ); 
ini_set('sendmail_from', 'user@example.com');
 
/* Define the headers, you might be able to condition the values if needed. In this example ill just use static values */
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Text <Guys@example.com>' . "\r\n";
$headers .= 'Cc: cc@example.com, cc1@example.com' . "\r\n";
$headers .= 'Bcc: bcc@example.com, bcc2@example.com' . "\r\n";
 
$to = 'some.one@some.domain.ext';
$subject = 'Explain the mail command ;-)';
$message = '<html><body><div>This is an HTML mail</div></body></html>';
 
/* If all data is complete? Execute the actual mail... */
mail($to, $subject, $message, $headers);
?>

Open in new window

0
 
Alicia St RoseOwner & Principle Developer/DesignerAuthor Commented:
This solution was the quickest and most efficient. It worked wonderfully, but then I realized I needed to validate whether all of the fields had been accurately filled and found some other code. I'm still having problems with a checkbox result showing up in the email. I'll post another question for that.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now