Jump to content

PHP Send Mail


fatboymills

Recommended Posts

Ok, how about this one...

 

I want to send the contents of my form back to the users email as well as mine. My Email is written at the top of the code to send the form to, but I want him to get the same copy of the email. I have the variable $Email for the users email...

 

$EmailTo = "nigelfatboy@Hotmail.com";

 

    and....

 

$Body .= "Email: ";

$Body .= $Email;

 

    and at the bottom....

 

$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

 

There must be some way to put the $Email variable in the mail or $EmailTo so it sends the user a copy, No?

Link to comment
Share on other sites

Am I looking at the wrong code? Which argument? The one in MY script or the Manual?

I asked:

 

Have you looked at the first argument in the manual? http://php.net/mail

This is the description given for the first argument `to`.

 

to

Receiver, or receivers of the mail.

 

The formatting of this string must comply with » RFC 2822. Some examples are:

 

user@example.com

user@example.com, anotheruser@example.com

User <user@example.com>

User <user@example.com>, Another User <anotheruser@example.com>

Still stuck?

Link to comment
Share on other sites

Am I hearing that you want us to help you pull an email address out of the body of some text from the form?  Basically, that the 'other' email address is not coming to your script as a distinct element that you can simply add to your headers?

 

UPON FURTHER REVIEW:

 

I see now that you are posting the other email value($Email) to the body of your mail.  Assuming that this is after you have processed the form and are preparing to send the mail, you REALLY need to read the manual as I suggested to see where you ought to be placing that variable, assuming that you have properly vetted it as valid input.

Edited by ginerjm
Link to comment
Share on other sites

We can solve your problem right now but then when you have another problem, where will you be?  Read the manual where it discusses how to use the mail function and you will be as smart as us when it comes to using mail()..  Where will that put you?

 

You make it sound like such hard work..... :)

Edited by ginerjm
Link to comment
Share on other sites

Assuming you have the two emails addresses you want to send to in the variables $Email and $EmailTo. To simply send the email to both of these addresses, just separate them by a comma as indicated in plain English in the manual page I linked to and the particular section I actually copied and quoted for you.

 

$success = mail("$Email,$EmailTo", $Subject, $Body, "From: <$EmailFrom>");
Link to comment
Share on other sites

Since you can't read English - or are afraid of manuals - you probably did screw it up.  Either show us the code you tried (with an echo of the values used) or don't bother the people here with your attitude anymore.

 

You made one of the most respected members here ignore you - that's quite an accomplishment for a newcomer.  Says a lot about you

Link to comment
Share on other sites

Ok, can somebody send my apologies to trq. I'm just so frustrated with this script. I didn't mean to offend. I appreciate the help guys. I need all the help I can get. Even though PHP frustrates me I love what this can do and enjoy scripting with it.

I don't need to burn bridges with you guys.

Sincerely, Fatboy.

Link to comment
Share on other sites

So - now that we're past that - can you post the code you last tried along with the contents of the vars used to send the mail?  You went wrong somewhere if you had to resort to calling the mail() function twice.

Link to comment
Share on other sites

This is what I have, however it DOES work. It sends results to me and also the form submitter... 

 

 
<?php
if(isset($_POST['email'])) {
     // EDIT THE 2 LINES BELOW AS REQUIRED
  $email_to = "fatboymills@hotmail.com";
  $email_subject = "Needs Result";
  function died($error) {
  // your error code can go here
  echo "Hmm, there were error(s) found with the form you submitted. ";
  echo "These errors appear below.<br /><br />";
  echo $error."<br /><br />";
  echo "Please go back and fix these errors.<br /><br />";
  die();
  }
  // validation expected data exists
  if(!isset($_POST['first_name']) ||
       !isset($_POST['last_name']) ||
   !isset($_POST['email']) ||
   !isset($_POST['telephone']) ||
   !isset($_POST['comments'])) {
   died('We are sorry, but there appears to be a problem with the form you submitted.');
   }
   $first_name = $_POST['first_name']; // required
   $last_name = $_POST['last_name']; // required
   $email_from = $_POST['email']; // required
   $telephone = $_POST['telephone']; // not required
   $comments = $_POST['comments']; // required
   $error_message = "";
   $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
   if(!preg_match($email_exp,$email_from)) {
   $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
   }
   $string_exp = "/^[A-Za-z .'-]+$/";
   if(!preg_match($string_exp,$first_name)) {
   $error_message .= 'The First Name you entered does not appear to be valid.<br />';
   }
   if(!preg_match($string_exp,$last_name))
   {
   $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
   }
   if(strlen($comments) < 2) {
   $error_message .= 'The Comments you entered do not appear to be valid.<br />';
   }
   if(strlen($error_message) > 0) {
   died($error_message);
   }
   $email_message = "Results below.\n\n";
   function clean_string($string) {
   $bad = array("content-type","bcc:","to:","cc:","href");
   return str_replace($bad,"",$string);
   }
   $email_message .= "First Name: ".clean_string($first_name)."\n";
   $email_message .= "Last Name: ".clean_string($last_name)."\n";
   $email_message .= "Email: ".clean_string($email_from)."\n";
   $email_message .= "Telephone: ".clean_string($telephone)."\n";
   $email_message .= "Needs: ".clean_string($comments)."\n";
   // create email headers
   $headers = 'From: '.$email_from."\r\n".
   'Reply-To: '.$email_from."\r\n" .
   'X-Mailer: PHP/' . phpversion();
   @mail($email_to, $email_subject, $email_message, $headers );
     @mail($email_from, $email_subject, $email_message, $headers );
   ?>
   <!-- include your own success html here -->
   Thanks, your Need has been sent.
   <?php
   }
   ?>
Link to comment
Share on other sites

1 - please use the proper code tags when posting code on a forum.  On this one it is the word 'code' in square brackets with a closing tag of "/code" in brackets also.

2 - very bad form to develop code with functions buried in the middle of main stream (or any other code for that matter) code.  Place them away from the rest of your code

3 - Why do you suppress the result of the call to mail()?  How can you even possibly follow a suppressed statement with a message of success???

 

I had wished that you posted the code where you tried to send both emails in one call.  That was your last complaint and that was I asked to see.

Edited by ginerjm
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.