how to make my website contact form to multiple recipients (emails) - php

how can i make my web contact form send request to multiple emails and each emails will not see the others receives email that receives the same request from the web contact form. My code is working fine but just only sending the request from the contact form to only one email address, how can i send it to multiple emails this is my code below :
<?php
$name = $_POST['name']; // form field
$email = $_POST['email']; // form field
$message = $_POST['message']; // form field
if ($_POST['submit']){
$from = "Contact us<info#xxxxxx>"; //enter your email address
$to = "xxxxxx#outlook.com"; //enter the email address of the contact your sending to
$subject = "you have 6 pending mail"; // subject of your email
$bcc = "";
$headers = array ('From' => $from,'To' => $to, 'Subject' => $subject, 'headers' => $headers);
$text = ''; // text versions of email.
$html = "<html><body>
Name: $name <br> Email: $email <br>Message: $message <br>
</body></html>"; // html versions of email.
$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
//do not ever try to call these lines in reverse order
$body = $mime->get();
$headers = $mime->headers($headers);
$host = "localhost"; // all scripts must use localhost
$username = "xxxxxxx"; // your email address (same as webmail username)
$password = "xxxxxx"; // your password (same as webmail password)
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true,
'username' => $username,'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
}
else {
echo("<p>Message successfully sent!</p>");
// header("Location: http://www.example.com/");
}
}
?>
<form action="qservers_mail.php" method="post">
<table border="0" style="background:#ececec" cellspacing="5">
<tr align="left"><td>Name</td><td><input type="text" size="30" name="name"></td></tr>
<tr align="left"><td>Email address</td><td><input type="text" size="30" name="email"></td></tr>
<tr align="left"><td valign="top">Comments</td><td><textarea name="message" rows="6" cols="30"></textarea></td></tr>
<tr align="left"><td> </td><td><input type="submit" value="Send" name='submit'></td></tr>
</table>
</form>

Just write them separated by commas.
$to = "first#mail.ru, second#mail.ru";

Related

How to send two emails in a single contact form using PEAR Mail

I have a contact form and I want to receive message through mail and simultaneously send mail to the client. I have the below code it works some times. That is some times I get the message and client also get the message but sometimes only one email is sent either to client or to me but never both all the time.
I have seen example in the following link https://teamtreehouse.com/community/how-to-send-two-different-email-with-different-bodies-using-phpmailer but it uses PHP Mailer I want the same functionality but by using PEAR Mail.
I have seen other similar question over here PEAR Mail using gmail SMTP won't send 2 emails in sucession but the answer given in that question doesn't solve my problem
Below is the code which works some time.
<?php
require_once('Mail.php');
require_once('Mail/mime.php');
if (isset($_POST['Captcha'])) {
$name = filter_var($_POST['Name'] , FILTER_SANITIZE_STRING);
$email = filter_var($_POST['Email'] , FILTER_SANITIZE_EMAIL);
$phone = filter_var($_POST['Phone'] , FILTER_SANITIZE_STRING);
$budget = filter_var($_POST['Budget'] , FILTER_SANITIZE_STRING);
$timeline = filter_var($_POST['Timeline'] , FILTER_SANITIZE_STRING);
$description = filter_var($_POST['Description'] , FILTER_SANITIZE_STRING);
$spam = filter_var($_POST['usernameoftest'] , FILTER_SANITIZE_STRING); // Bot trap
if($spam)
{ // If the hidden field is not empty, it's a bot
die("No spamming allowed bitch!");
} else {
$from = "sales#ganeshghate.com";
$to = "ganeshghate#hotmail.com";
$subject = "Message from : ".$name;
$body = "Client's Name : ".$name."\r\n";
$body .= "Client's Email ID : ".$email."\r\n";
$body .= "Client's Phone Number : ".$phone."\r\n";
$body .= "Client's Budget : ".$budget."\r\n";
$body .= "Client's Timeline : ".$timeline."\r\n";
$body .= "Client's Message : ".$description."\r\n";
$host = "myhost";
$port = "myport";
$username = "myusername";
$password = "mypassword";
$crlf = "\n";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$mime = new Mail_mime($crlf);
$mime->setTXTBody($body);
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
$output["errorclientemail"] = "<div>
<h2 class='error'>".$mail->getMessage().$mail->getUserInfo()."</h2>
<h2>Oops there was some error we have not received your message</h2>
<h2>Please report the above error to us via skype , email , phone details are on the left side</h2>
</div>"."\r\n";
} else {
$output["successclientemail"] = "Thank you we will get back to you soon";
}
$bodyback = "We have received your message :".$body."\r\n";
$bodyback .= "We will get back to you soon"."\r\n";
$bodyback .= "With Regards "."\r\n";
$bodyback .= "Ganesh Ghate "."\r\n";
$subjectback = "Thank You for contacting us";
$headersreply = array ('From' => $from,
'To' => $email,
'Subject' => $subjectback);
$crlf1 = "\n";
$mime = new Mail_mime($crlf1);
$mime->setTXTBody($bodyback);
$bodyback = $mime->get();
$headersreply = $mime->headers($headersreply);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($email, $headersreply, $bodyback);
if (PEAR::isError($mail)) {
$output["errorreplyemail"] = "<div>
<h2 class='error'>".$mail->getMessage().$mail->getUserInfo()."</h2>
<h2>Oops there was some error we have not delivered confirmation email to you</h2>
<h2>Please report the above erorr to us via skype , email , phone details are on the left side</h2>
</div>"."\r\n";
} else {
$output["successreplyemail"] = "We have sent confirmation email to you. Please check your inbox / junk mail";
}
echo json_encode($output);
}
}
?>
Thanks in advance

Using PHP Pear mail :Failed to add recipient:

I am using Php Pear mail for sending an attachment to the user who fills out a form. If I hard code the "to email" address it works fine. But when I use
$to = $_POST['email'] ; I get the following error.
Failed to add recipient: #localhost [SMTP: Invalid response code received from server (code: 501, response: <#localhost>: no local part)]
<?php
require_once 'Mail.php';
require_once 'Mail/mime.php';
$from = "email#domain.com";
$to = $_POST['email'] ;
$subject = 'Free Diagnostic Test Coupon';
$headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);
$text = 'Please find attached the coupon';// text and html versions of email.
$html = '<html><body>Please find attached the coupon</body> </html>';
$file = 'img/coupon.jpg'; // attachment
$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'image/jpeg');
//do not ever try to call these lines
$host = "host";
$username = "username";
$password = "password";
in reverse order
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true,
'username' => $username,'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
}
else {
echo("<p>Message successfully sent!</p>");
}
?>
Any help will be appreciated.
Check your code by adding the top of it.
error_reporting(E_ALL); ini_set('display_errors', '1');

My php mail function is not working?

Ive got the following php code:
<?
if ($_POST['emailme']) {
$yemail = $_POST['email'];
$host = $_SERVER['HTTP_HOST'];
$email = "<autoreply#$host>";
if (mail('$yemail', 'This is a Subject', 'This is the body of the email', 'From: $email')) {
echo "Message sent!";
} else {
echo "Message failed sending!";
}
}
?>
This is my HTML:
<FORM METHOD="POST" ACTION=""><INPUT TYPE='TEXT' CLASS='BOX' NAME='email' /><INPUT TYPE='SUBMIT' NAME='emailme' CLASS='SUBMITBOX' VALUE='Send!' /></FORM>
Any ideas why its not sending the email ? it says Message Sent but im not receiving any emails in my inbox
All help is much appreciated, thanks
PS: ive tried the following (with double quotes):
if (mail("$yemail", "This is a Subject", "This is the body of the email", "From:" . $email)) {
echo "Message sent!";
} else {
echo "Message failed sending!";
}
but still no luck
try this
if (mail('akh40#hotmail.co.uk', 'This is a Subject', 'This is the body of the email', 'From:'. $email))
Apparently most hosting companies are dropping support for php's mail() function in favour of SMTP versions of email scripts.
See http://www.thesitewizard.com/php/protect-script-from-email-injection.shtml for an explanation of why.
Try this for an SMTP/SSL script:
<?php
require_once "Mail.php";
$from = "Sandra Sender <sender#example.com>";
$to = "Ramona Recipient <recipient#example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://mail.example.com";
$port = "465";
$username = "smtp_username";
$password = "smtp_password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>

Cannot get email from query string

I'm having trouble getting an email address from $_GET.
Here is my code:
<?php
$eadd = $_GET['email'];
echo("<p>Please check your inbox on your email $eadd.</p>");
?>
I went to this link:
http://localhost/file.php?email=myemail#company.com
Yet the output is only:
Please check your inbox on your email .
EDIT
Here's my complete code :
<?php
require_once "Mail.php";
//Get link posted info's needed
//Email
$eadd = $_GET['email'];
$from = "OtakuJam Registration <no-reply#comp.com>";
$to = " $unick < $eadd >";
$subject = "Thank you for registering";
$body = "Dear $unick ,
\n Thank you for registering to OtakuJam. To activate your account, \n
";
$host = "mail.srv.com";
$username = "name#comp.com";
$password = "mypass";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Please check your inbox on your email $eadd.</p>");
}
?>
`Please ignore the $unick there, also the mail won't send unless i put an email on the code instead of < $eadd >
turning my $to syntax to $to = $unick . "<" . $eadd . ">"; as #Charlie gave me helped me. Also to those who told me to use ".$variable.". to my echo helped. thanks

PHP get form not receiving email

I'm using a simple web form and some PHP script to send the completed form to my email. When I hit "Submit" I get the PHP text "Thanks, we'll be in touch shortly" but I'm not getting the email with submitted form info. Not sure what's the problem.
from the HTML doc:
<div class='' "letstalk_form">
<form method="get" action="contact.php" name="contact" id="contact">
<p>
<label for="txtfname">Name:</label><input type="text" tabindex="1" name="txtfname" id="txtfname" class="clsTextBox">
</p>
<p>
<label for="txtcompany">Company:</label><input type="text" tabindex="2" name="txtcompany" id="txtcompany" class="clsTextBox">
</p>
<p>
<label for="txtemail">E-mail:</label><input type="text" tabindex="3" name="txtemail" id="txtemail" class="clsTextBox">
</p>
<p>
Message:<textarea name="comments" id="comments" rows="2" cols="20"></textarea>
</p>
<p class="submit">
<input type="submit" value="Submit">
</p>
</form>
</div>
and the PHP
$msg = "Sender Name:\t$txtfname\n";
$msg .= "Sender Company:\t$txtcompany\n";
$msg .= "Sender E-mail:\t$txtemail\n";
$msg .= "Comments:\t$comments\n\n";
$recipient = "receiversemail#somewhere.com";
$subject = "from PI Website";
$mailheaders = "From: Lets Do Business <> \n";
$mailheaders .= "Reply-To: $txtemail\n\n";
mail($recipient, $subject, $msg, $mailheaders);
echo "<HTML><HEAD><TITLE>Form Sent!</TITLE></HEAD><BODY>";
echo "<H3 align=center>Thank You, $txtfname</H3>";
echo "<P align=center>Your submission was sent successfully.<br />
I will be contacting you soon.</P>";
echo "</BODY></HTML>";
?>
</div>
I've used this is the past and worked great. Is there something I overlooked? Thanks for you help!
EDIT: I ended up using the link Turgut Dursun provided (html-form-guide.com/contact-form/php-email-contact-form.html ) to set up the form. Works great. Thanks.
Are you assigning the $_GET[] values from the form to variables prior to this portion of your code?
$msg = "Sender Name:\t$txtfname\n";
$msg .= "Sender Company:\t$txtcompany\n";
$msg .= "Sender E-mail:\t$txtemail\n";
$msg .= "Comments:\t$comments\n\n";
$recipient = "abosiger#embarqmail.com";
$subject = "from PI Website";
$mailheaders = "From: Lets Do Business <> \n";
$mailheaders .= "Reply-To: $txtemail\n\n";
mail($recipient, $subject, $msg, $mailheaders);
If not, you need to either initialize your $_GET[] values to individual variables:
$txtfname = $_GET['textfname'];
for each variable sent through get. Or you can access them directly from the $_GET array through the following syntax:
$msg = "Sender Name:\t" . $_GET['txtfname'] . "\n";
Let me know if this helps.
<?php
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
change it like this.
The problem probably comes from a bad configuration of the mail function. You have to configure it properly.
Also, you should provide an email in the "From" header.
if message sent and no mails is receieved, this authenticating issues from your domain
... your script might works fine
The two main ways to do this is either using PHPMail() or SMTP. PHPMail() is open and used by trojans and viruses. Most clients have infected files which just send mails using PHPMail(), because it doesn't authenticate. It doesn't check if the email being sent is actually coming from the domain and this puts a lot of strain on the mail server, making legitimate mails wait in queue for a long time.
If you already have a script using PHPMail() function it is very simple to change it to SMTP, you just need to change like 5 lines of your code and add an email address and password (already created in cpanel) into the code, and it will send emails just fine.
if ($_POST['submit']){
$from = "slick#meetslick.com"; //enter your email address
$to = "slickbozz#gmail.com"; //enter the email address of the contact your sending to
$subject = "Contact Form"; // subject of your email
$headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);
$text = ''; // text versions of email.
$html = "<html><body>Name: $name <br> Email: $email <br>Message: $message <br></body></html>"; // html versions of email.
$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
//do not ever try to call these lines in reverse order
$body = $mime->get();
$headers = $mime->headers($headers);
$host = "localhost"; // all scripts must use localhost
$username = "support#meetslick.com"; // your email address (same as webmail username)
$password = " here "; // your password (same as webmail password)
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true,
'username' => $username,'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
}
else {
echo("<p>Message successfully sent!</p>");
// header("Location: http://www.meetslick.com/");
}
}
I hope this will be helpful to someone one day.

Categories