0

I am looking to use this php mail handler to send an email to both my email address and the email address that is submitted through a web form by the user. It is successfully sending it to my email address (me@example.com), however, what I am having trouble with is also sending it to the email address that is input by the user. Any help is greatly appreciated!

<?php
if(isset($_POST['Submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$to='me@example.com'; 
$subject='Your 25% OFF discount code!';
$message="Email: ".$email."\n\n"."Name: ".$name."\n\n"."25% Discount Code: Discount25";
$headers='From: me@example.com';

if(mail($to, $subject, $message, $headers)){
        header("Location:http://www.example.com/thankyou");     
}
else{echo "Something went wrong!";}
}
?>
natalie
  • 41
  • 5
  • Have you tried concatenating your email address such as `$to = "me@example.com,{$email}";` ? – Igor Ilic Jan 15 '19 at 07:49
  • Just tried that, it is still only sending to my email address :/ – natalie Jan 15 '19 at 07:54
  • According to the [PHP manual page](https://secure.php.net/manual/en/function.mail.php) the parameter `$to` accepts comma separated list of email addresses. Ex: `user@example.com, anotheruser@example.com` or `User , Another User ` – Igor Ilic Jan 15 '19 at 07:58
  • That is correct. However I am wondering if it accepts the variable email address that is input into the email form field by the user? And how would it be concatenated... – natalie Jan 15 '19 at 08:01
  • Yes, that is possible, you can do it like [this](https://stackoverflow.com/a/5368897/5914775) or [this](https://stackoverflow.com/a/8336881/5914775). (yes, I know I should not answer in the comments, but this is a lot easier than to write an actual answer or mark this question as a dupe) – Tom Udding Jan 15 '19 at 08:42
  • What I did was assign my email to $email2 and then use $to="{$email},{$email2}"; to send to both my email and the user's email and that seemed to do the trick. @IgorIlic thanks for your help! – natalie Jan 15 '19 at 19:43

0 Answers0