php send mail to both parties POST collected email address - php

This is probably an easy question but I am not sure the proper syntax.
I am sending an email to my client - but my client also wants that person to receive the emails once they hit submit.
Currently its a POST php form and it works fine - but need to include the POST-email address that was entered into the form.
$from_email = $_POST['emailAddress'];
$to = 'owen#gmail.com';
AND I have mail($to, $subject, $message, $header..
So how do I rewrite the code:
$to = 'owenpiccirillo#gmail.com';
to have also send to the email submitted in the form? because this works.
$to = 'owen#gmail.com, whatever#aol.com';
but this doesnt work....
$to = 'owen#gmail.com' . $from_email;
Thanks in advance
-O

You need a comma and space in the $to concatenation:
NOT
$to = 'owen#gmail.com' . $from_email;
// results in 'owen#gmail.comwhatever#aol.com'
but THIS
$to = 'owen#gmail.com, ' . $from_email;
// results in 'owen#gmail.com, whatever#aol.com'

If this works:
$to = 'owen#gmail.com, whatever#aol.com';
then what exactly is the issue? The reason this doesn't work:
$to = 'owen#gmail.com' . $from_email;
is because there's no comma separating the values. So it would evaluate to:
$to = 'owen#gmail.comwhatever#aol.com';
which isn't a valid email address.

This is because the . is concatenating the two addresses together. So if you do an echo or print_r you would see something like this:
owen#gmail.comwhatever#aol.com
You need to add a comma:
// Value will be injected into the string with double quotes.
$to = "owen#gmail.com, $from_email";
Alternatively, you can use CC and BCC to send copies.

Related

PHP not encoding quoted_printable_encode correctly

I'm trying to add an emoticon(fire) in email subject. Below is the code I'm using:
$subject = "Test email \xF0\x9F\x94\xA5";
$charset = "UTF-8";
$subject = sprintf('=?%s?Q?%s?=', $charset, quoted_printable_encode($subject));
// send email....
This is adding a fire emoticon to the email subject successfully. But when I try to get the subject from database like:
$subject = $themeArray['templateSubject']; // Test email \xF0\x9F\x94\xA5
$charset = "UTF-8";
$subject = sprintf('=?%s?Q?%s?=', $charset, quoted_printable_encode($subject));
// send email....
This is not converting the characters to emoticon. Instead I'm getting the subject in email as
Test email \xF0\x9F\x94\xA5
Why its not converting when I get subject from database?
Can anyone help me to fix this? Thanks in advance.

Send copy of email to BC email using Using mail() in php

If the clients email id is fatched using below code.
$message .= 'Email: '.$_POST['email']. "\n\r";
I guess $email is variable for client's email id. How do I send an email as BC to the client's email id?
Right now I am using only single email id using
$to = 'emailid#domain.com';
Update:
The email field value is <?php echo $_SESSION['email']; ?> in the form.
Update: I have pasted entire code at http://pastebin.com/5bWDQSk2
If you're running at least 4.3, you can set a Bcc additional header. PHP will interpret this internally before sending the message.
$headers = "Bcc: address#place.com\r\nFrom: me#place.com"; // etc
mail( $to, $subject, $message, $headers );
See the docs: http://www.php.net/manual/en/function.mail.php
Also note that because PHP doesn't expand escape sequences (\r, \n) when using single quotes, you must use double quotes for this string.
You have to add custom headers:
# bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
mail('recipient#host.com', 'Subject', 'Message', 'Bcc: emailid#domain.com' )
Add the BCC as a header e.g.
$header = "Bcc: john.doe#yahoo.com";

Email Form more than 20 values

I have a form with more than 20 input fields. The PHP mail function is not working since it can only accept 5 parameters. How can I send these values to my email address?
$to = 'myemail#mydomain.com';
$subject = 'form values';
$message = '';
foreach( $_POST as $key => $ value ) {
$message .= $key . ' => ' . $value . '<br>';
}
mail( $to, $subject, $message);
The parameters in the PHP mail() function have different meaning. They are not the data, sent to the user. Try using the following script and if it works - format the $message to match your requirements.
$email = 'rec#example.com';
$subject = 'Subject';
$message = print_r($_POST,true);
mail($email,$subject,$message);
More information can be found # http://php.net/manual/en/function.mail.php
Well I don't know what kind of input fields you have, but they could all be passed in the "subject" field. However, if you have a sufficiently complex email to send, and you really need to write everything yourself, you should look into PEAR.
So either add the 20 input fields onto the subject $subject .= 20things or use a tool that is inherently secure without having to remember to escape input and validate using regex.

PHP foreach loop inside foreach loop

I'm trying to do a foreach loop inside a foreach loop.
I have a form where the user type in some text and hit send. On the server site I loop through an array with email addresses and send out the text message.
Now I also want the user to be able to use variables in the textarea, like $name.
So my loop should first loop through the emails, and then str_replace the userinput $name variable, with the names in my array.
The loop works fine with the email part ($tlf), but not the replace $name part.
Could some spot what I am doing wrong?
$message = stripslashes(strip_tags($_POST['message2']));
$tlf=array("name1","name2");
$test=array("mname1", "mname2");
$subject = "Hello world";
$from = "me#gmail.com";
$headers = "From: $from";
foreach($tlf as $name){
$to = $name. "#gmail.com";
foreach($test as $navn){
$message = str_replace('$navn', $navn, $message);}
mail($to,$subject,$message,$headers);
}
Thanks a lot.
EDIT:
The output is an email sent. Say the user type in "hello $name".
I want it to first loop through the $tlf array, in this case creating 2 emails. This goes in as $to in the first loop. This works.
Now the next loop should recognize the user input "hello $name" and loop through the $test array replacing the user $name variable.
The output would be 2 emails send.
Mail output:
to: name1#gmail.com
message: hello mname1
Mail output:
to: name1#gmail.com
message: hello mname2
Let me know if I need to explain better, its hard for me to explain sorry.
When you do the following:
str_replace('$navn', $navn, $message)
Then all literal occurences of $navn will be replaced (the first, the second, the third, ...). So running through that loop a second time can't possibly replace anything more.
You will need to define two placeholders, or make some distinction, or use preg_replace_callback if you want to declare in which order (or other logic) the possible replacement strings are applied.
If you had told us (but you haven't) you only wanted to replace the first occurence in each iteration, then a normal preg_replace(.., .., .., 1) would work.
Is this what you want?
$message = stripslashes(strip_tags($_POST['message2']));
$tlf=array(
array("email" => "name1", "name" => "mname1"),
array("email" => "name2", "name" => "mname2")
);
$subject = "Hello world";
$from = "me#gmail.com";
$headers = "From: $from";
foreach($tlf as $contact){
$to = $contact["email"] "#gmail.com";
$replacedMessage = str_replace('$navn', $contact["name"], $message);
mail($to,$subject,$replacedMessage,$headers);
}

How to have a variable in a mail line

okay, here is the code
while (!feof($text)) {
$name = fgets($text);
$number = fgets($text);
$carrier = fgets($text);
$date = fgets($text);
$line = fgets($text);
$content = $_POST['message'];
$message .= $content;
$message .= "\n";
$number = $number;
mail("$number#vtext.com", "Event Alert", $message, "SGA");
Header("Location: mailconf.php");
}
I am trying to get a message sent to a phone, I have 'message' coming in from a text area, and then I assign it to "$content" then I put "$content" into "$message" and then as you can see, in my mail line, I want the address to be the variable "number"#vtext.com I have the while loop because there are many people in these files I wish to send a message to...
When I change the "$number#vtext.com" to my email address, it sends everything fine, I just cannot seem to get it to send to a phone number, please help!
thanks!
The problem is that fgets includes the newline character as part of the return. So you are effectively sending an email to:
1234567890
#vtext.com
Which of course is invalid. Change your line that reads $number = $number to:
$number = trim($number);
And the rest of your code should function as expected, with the email being received on your phone.
Part of my original answer
I would highly recommend using a (probably paid) service to send SMS messages to phones if you need anything remotely reliable. A quick search yielded a few results:
PHP/SMS Tutorial
A list of 5 ways to Text for free <-- This link is dated, but might still be helpful
You need to append the number variable and the "#vtext.com" literal string together:
mail($number . "#vtext.com", "Event Alert", $message, "SGA");

Categories