php email - $to multiple recipients - php

Even this must have been be asked many times, I will ask again since I cannot get it to work.
I am using php mail($to, $subject, $message, "From: $mysite<$myemail>\nX-Mailer:PHP/" .phpversion()); to send email to a single recipient.
Now I need to sent it to more than one recipients. I know that normaly I could do:
$to = "emailA#here.com,emailB#there.com";
But I need the one of the recipients to be the user that fills in the form e.g.:
//get all form details
$email = $_POST['email'];
$to = "$email,emailB#there.com";
The above ($to) I don't know if it is correct or not but is not working for me...
If I leave only the $to = "$email"; it gets send to $email (meaning that my rest of the code is ok).
Any suggestion on what is or may be wrong here?
Thank you.

Add a CC to your header.
$header ="From: $mysite<$myemail>" . PHP_EOL;
$header .= 'CC: emailB#there.com' . PHP_EOL;
//Rest of headers here

Related

Sending email from html form using php [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
Ive been trying this out the whole day but I cant figure out how to send an email from my html contact form containing the information from the form to my email address. Im new to php.
Ive tried running this by uploading it to free web hosting. I get the message "success!" when I press the submit button on my html form but no email is actually sent.
Any help is appreciated.
PHP script:
<?php
//Subject
$subject ="Contact Form Submission";
// Name
$name =$_POST['InputName'];
// Message
$message =$_POST['InputMessage'];
//Mail of Sender
$email =$_POST['InputEmail'];
//From
$header = "From:$name<$email>";
$send_contact=mail("myemail#gmail.com",$subject,$message,$header);
//Check if mail was sent
if($send_contact){
echo "Success!";
}
else {
echo "Error!";
}
?>
EDIT: Figured it out after one whole day of trial and error. The problem was with the free web host I was using. Changed hosts and the code started working fine. Hope this helps someone in the future. Thanks all for the help.
I have a pretty good idea why your code is not working. It happened to me a long time ago. The reason why your code is not working is because :
When you pass "from" in headers, php expects an existing email account of your
server. For example : $headers = 'From: emailacc#yourserver.com';
So first thing you gotta do is create an email account on your server. And then put the From in header to the email address that you've just created.
The From field in the $headers is not the From as you think.
<?php
$email = $_POST["InputEmail"];
$subject = $_POST["InputSubject"];
$message = "From: ".$email.", ".$_POST["InputMessage"]; // you put the email address from the input form here
$headers = 'From: emailacc#yourserver.com'; // here is the email address specified from which u want to send the email.(i.e. your server email address)
mail($to, $subject, $message, $headers)
?>
I'm sure this will do the job :)
Have a shot at this.
I changed you're $header variable around a little bit, so that rather than setting the email as "$email", It'll actually pass through the posted email entered in the form. This apply's to the name too.
I also made it so that you pass the mail function through the parameters of the if statement, rather than setting a new variable.
$headers = "From: " . $name . "<" . $email . ">"; // notice new concatenation
if(mail("myemail#gmail.com", "Contact Form Submission", $message, $headers)){
// success message
} else {
// error message
}
Really hope this helps! :)
Try adding spaces after the "=" that might be the problem,
If that doesn't work you could try to use this
<?php
$emailvariable = $_POST['InputEmail']
$to = 'example#gmail.com';
$subject = "Form"
$message = $_POST['InputMessage'];
$headers = "From: $emailvariable";
mail($to, $subject, $message, $headers);
?>
Hope this helps

send private copy of autoresponder mail

I have some php here that works great. I want a mail to be sent to the user who submits a form and I also want a copy of that mail sent to myself but I don't want my email address to be made available to the user.
Here's the php I'm using to govern the mail sending ...
$to = 'xxxx#xxxx.com' . ', ';
$to .= $email;
$subject = 'xxxx';
$message = "Thank you for submitting the form.";
$headers = "From: xxxx#xxxx.com\r\nReply-To: xxxx#xxxx.com";
$mail_sent = #mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
When the code is parsed emails are duly sent to both the users submitted email ($email) and the address I enter in the first $to variable however the user can see the email address I enter as another recipient when they receive the email. Anyone know how I can get around this? Any help will be much appreciated. Thanks.
Use a BCC header instead of an additional To in your $headers string. It stands for "Blind Carbon Copy", and instructs the mail server to duplicate the mail to extra recipients, but remove that header from the original copy, so the main recipients can't know it was there.

Need a way to set a form submission to show it is coming from the sender using email field

I have a form that people fill out. It sends me an email when they submit. I want the email to show it is from the person sending it. Their email is one of the fields. I want to be able to hit reply to reply to them.
This is the code I am using:
mail($_POST["e_mail"]."myaddress#email.com", $_POST['e-mail']." Interested in Cajun Catering", "Name: ".$_POST['Name'] ."\n"."Address: ".$_POST['address']."\n"."City: ".$_POST['city']."\n"."E-Mail: ".$_POST['e_mail']."\n"."Phone: ". $_POST['phone']."\n"."Time to call: ".$_POST['call']."\n"."Products: ".$_POST['Prod1']." ".$_POST['Prod2']." ".$_POST['Prod3']." ".$_POST['Prod4']." \n"."Type: ".$_POST['type']."\n"."Event Date: ".$_POST['event_date']."\n"."Number of Guests: ".$_POST['Number']."\n"."Comments: ".$_POST['comments'],'From: '.$_POST['e_mail']);
You could try using the -f option with mail():
$to = "youremailaddress#email.com";
$subject = "The subject";
$message = "The message";
$from = "FROM: returnaddress#email.com";
$return = "-freturnaddress#email.com"
mail($to, $subject, $message, $from, $return);
Note that $return contains the same email address as $from, preceded by -f

Email header not working PHP

I'm using BlueHost and I can't seem to get my email form to work. This is the PHP:
$to = "test#email.com";
$subject = "test";
$message = "test message";
$from = $_POST['cf_email'];
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo $headers;
This doesn't seem to send the email. However, if I add ANY string in front of the email, then it works. e.g.:
$from = "zz".$_POST['cf_email'];
Can anyone tell me what I'm doing wrong? Thanks.
You cannot use any personal email ($_POST['cf_email']) in the 'From' header.
Also you have to add 'reply-to'.
Please check below thread for more info.
problem with php mail 'From' header

PHP mail function user can't receive the mail if I added .com.sg instead of just .com

I want to send the user an activation link after they registered an account. when I put this http://www.homeloan.com.sg in the $message I didn't receive the email, but when I remove the .sg and put http://www.homeloan.com it works. There's no error message, so I really don't know what's my mistake. Please help
here are my codes:
$id = mysql_insert_id();
$to = 'myemail#gmail.com';
$subject = "E-mail Verification";
$message = 'Click on the link to verify your account-> http://www.homeloan.com.sg/rates/activate?id='.$id.'';
$headers = "From: Homeloan Singapore" . "\r\n" . "Reply-To: enquiry#homeloan.com.sg";
mail($to,$subject,$message,$headers, '-f enquiry#homeloan.com.sg');
Make sure if your site have a form to fill, then fill the form correctly by assembling the input tag in the corresponded variable.
Try concatenating 'the email' to the variables ($...) with (.) or "...".
I really don't know what's my mistake
There is no mistake. I tried this code:
<?php
$id = 1;
$to = 'my_email#gmail.com';
$subject = "E-mail Verification";
$message = 'Click on the link to verify your account-> http://www.homeloan.com.sg/rates/activate?id='.$id;
$headers = "From: Homeloan Singapore" . "\r\n" . "Reply-To: enquiry#homeloan.com.sg";
mail($to,$subject,$message,$headers, '-f enquiry#homeloan.com.sg');
It arrived in the mailbox. Maybe, for your account it was put into spam folder?

Categories