I'm working on a contact form for my website and I'm trying to make the user submit their email and name through an HTML form. My form has a reCaptcha and here is the action file:
<?php
if(isset($_POST['submit']) && !empty($_POST['submit'])):
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
// Your site secret key
$secret = 'my_secret_key';
// Remote user address
$ip = $_SERVER["REMOTE_ADDR"];
// Get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . $secret . '&response=' . $_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if($responseData->success):
// Contact form submission code
$email = htmlentities(!empty($_POST['email'])?$_POST['email']:'');
$to = 'myemail#gmail.com';
$subject = 'A new email from ' . $email;
$htmlContent = '<p>' . $email . '</p>';
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: User <' . $email . '>' . "\r\n";
// Send email
#mail($to, $subject, $htmlContent, $headers);
#mail($email, $subject, $htmlContent, $headers);
endif;
endif;
endif;
?>
So if the user submits an # (at) in their email field, the email never gets sent. I suspect it could be because of the above code. Are there any kind of restrictions to which characters can be sent/accepted by mail? And if so why? I tried sending a message like this:
адгтф~`!#$%^&*()_-+=<,>.?/:;"'\|{}[]
And it never got to my email-box. Help would be really appreciated!
Related
I have added a quick contact form in every page so clients can easily get in touch. However, three different things are happening as below:
sometimes getting inquiries from client. (Genuine I can say, we worked together)
sometimes getting blank message. with Unknown sender every alternate day.
Getting nothing. (Whenever I am trying to check whether all pages working or not. I am not getting email on first time. (Sometimes second, third, fourth time works).
My code is as follows:
PHP script I am using:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>' some message '
);
//echo '<pre>';print_r($_POST);exit;
$name = #trim(stripslashes($_POST['name']));
$email_from = #trim($_POST['email']);
$websitelink = #trim(stripslashes($_POST['websitelink']));
$subject = 'iMarketingJunkies: Inquiry';
$message = #trim(stripslashes($_POST['message']));
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <'.$email_from.'>' . "\r\n";
//$email_from = $email;
$email_to = 'myemail#email.com';//replace with your email
$body = 'Name: ' . $name . '<br><br>' . 'Email: ' . $email_from . '<br><br>' . 'URL: ' . $websitelink . '<br><br>' . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, $headers);
echo json_encode($status);
die;
?>
Can anybody please help me to get rid of this. I have tested all pages and I am sure we can't add submit button without any data. I am using Cloudfare.
Thank you
I am trying to send email to multiple recipients using PHP. My code is like this
<?php
foreach($subscribers as $subscriber){
$email[] = $subscriber['email'];
}
$emails = implode(',', $email);
$email_from = "email#example.com";
$subject = "My Subject";
$full_name = 'Example Sender';
$from_mail = $full_name.'<'.$email_from.'>';
$email_text = file_get_contents('email.html');
$headers .= 'From: ' . $from_mail . "\r\n".
"MIME-Version: 1.0" . "\r\n" .
"Content-type: text/html; charset=UTF-8" . "\r\n";
mail($emails, $subject, $email_text, $headers);
?>
I am able to send emails correctly. But it shows others email addresses in there email box and I don't want to share email addresses with each other. Please help how can I do this. I tried to send email inside the for each loop like this
<?php
foreach($subscribers as $subscriber){
$email = $subscriber['email'];
$email_from = "email#example.com";
$subject = "My Subject";
$full_name = 'Example Sender';
$from_mail = $full_name.'<'.$email_from.'>';
$email_text = file_get_contents('email.html');
$headers .= 'From: ' . $from_mail . "\r\n".
"MIME-Version: 1.0" . "\r\n" .
"Content-type: text/html; charset=UTF-8" . "\r\n";
mail($email, $subject, $email_text, $headers);
}
?>
But it sends email to only the first email address inside loop. Please help.
You can add an empty string in the $email field and use bcc headers to add the mail addresses. bcc makes sure the receiver can't see the other email addresses.
$headers .= "Bcc:email#example.com" . PHP_EOL;
There are two steps first the email i sent to the person who wants the newsletter and the second email needs to be sent to info#dirtytrend.com. The problem is that the email is sent in the first step but the email in the second step is never getting through but the code seems to carry on without any errors.
The code is below
<?php
$name = $_POST["nameofperson"];
$to = $_POST["emailofperson"];
$subject = "Hi!";
$body = "Hi " . $name . ",<br><br>Thank you for subscribing?\n\nWe have logged your email to process your newsletter and you will recieve an email from us confirming your subscription.";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if (mail($to, $subject, $body, $headers)) {
$body2 = "Name: " . $name . "\n";
$body2 .= "Email: " . $to;
if(mail("info#dirtytrend.com", "Subscription Request", $body2)){
header("Location: http://www.dirtytrend.com/events.html");
}
else{
}
} else {
echo "ERROR: Email not sent please contact the system administrator";
}
?>
In the second mail() you have only 3 parameters and you're missing the 4th one with the headers so maybe it is recognized as spam?
I'm using a basic PHP mail form on a website & my client has mentioned to me that he is getting a random email every day or so in his inbox that is totally blank. I read about the issue and was thinking that the mail form is sending an email every time the page is loaded, is that correct? Or is this another issue?
PHP
<?php
$mail_to = "email1#gmail.com, email2#gmail.com"; // specify your email here
// Assigning data from the $_POST array to variables
$name = $_POST['sender_name'];
$mail_from = $_POST['sender_email'];
$phone = $_POST['sender_phone'];
$web = $_POST['sender_web'];
$company = $_POST['sender_company'];
$addy = $_POST['sender_addy'];
$message = $_POST['sender_message'];
// Construct email subject
$subject = 'Web Prayer Request from ' . $name;
// Construct email body
$body_message = 'From: ' . $name . "\r\n";
$body_message .= 'E-mail: ' . $mail_from . "\r\n";
$body_message .= 'Phone: ' . $phone . "\r\n";
$body_message .= 'Prayer Request: ' . $message;
// Construct email headers
$headers = 'From: ' . $name . "\r\n";
$headers .= 'Reply-To: ' . $mail_from . "\r\n";
$mail_sent = mail($mail_to, $subject, $body_message, $headers);
if ($mail_sent == true){ ?>
<script language="javascript" type="text/javascript">
alert('Your prayer request has been submitted - thank you.');
window.location = 'prayer-request.php';
</script>
<?php } else { ?>
<script language="javascript" type="text/javascript">
alert('Message not sent. Please, notify the site administrator info#info.com');
window.location = 'prayer-request.php';
</script>
<?php
}
?>
Link to site: http://tinyurl.com/dy48jom
Your help is much appreciated!
You have to consider implementing captcha in your mail form so that automated scripts wont send mail from your domain .captcha also make sure that you make some of the fields as required (validate not only using js but also using server side php code).
I'm very new to PHP and am using a basic template 'send-mail' form on a contact page.
It's been requested that I send the email out to multiple email addresses when the "Submit" button is clicked. I've searched around & haven't quite found what I needed. What code do I need to add into the form below in order to send this out to multiple email addresses?
<?php
$mail_to = 'daniel30293#gmail.com'; // specify your email here
// Assigning data from the $_POST array to variables
$name = $_POST['sender_name'];
$mail_from = $_POST['sender_email'];
$phone = $_POST['sender_phone'];
$web = $_POST['sender_web'];
$company = $_POST['sender_company'];
$addy = $_POST['sender_addy'];
$message = $_POST['sender_message'];
// Construct email subject
$subject = 'Web Prayer Request from ' . $name;
// Construct email body
$body_message = 'From: ' . $name . "\r\n";
$body_message .= 'E-mail: ' . $mail_from . "\r\n";
$body_message .= 'Phone: ' . $phone . "\r\n";
$body_message .= 'Prayer Request: ' . $message;
// Construct email headers
$headers = 'From: ' . $name . "\r\n";
$headers .= 'Reply-To: ' . $mail_from . "\r\n";
$mail_sent = mail($mail_to, $subject, $body_message, $headers);
if ($mail_sent == true){ ?>
<script language="javascript" type="text/javascript">
alert('Your prayer request has been submitted - thank you.');
window.location = 'prayer-request.php';
</script>
<?php } else { ?>
<script language="javascript" type="text/javascript">
alert('Message not sent. Please, notify the site administrator admin#bondofperfection.com');
window.location = 'prayer-request.php';
</script>
<?php
}
?>
Your help is greatly appreciated.
You implode an array of recipients:
$recipients = array('jack#gmail.com', 'jill#gmail.com');
mail(implode(',', $recipients), $submit, $message, $headers);
See the PHP: Mail function reference - http://php.net/manual/en/function.mail.php
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>
Just add multiple recipients comma seperated in your $mail_to variable like so:
$mail_to = 'nobody#example.com,anotheruser#example.com,yetanotheruser#example.com';
See
mail() function in PHP
Here is a simple example:
<?php
// Has the form been submitted?
// formSubmit: <input type="submit" name="formSubmit">
if (isset($_POST['formSubmit'])) {
// Set some variables
$required_fields = array('name', 'email');
$errors = array();
$success_message = "Congrats! Your message has been sent successfully!";
$sendmail_error_message = "Oops! Something has gone wrong, please try later.";
// Cool the form has been submitted! Let's loop through the required fields and check
// if they meet our condition(s)
foreach ($required_fields as $fieldName) {
// If the current field in the loop is NOT part of the form submission -OR-
// if the current field in the loop is empty, then...
if (!isset($_POST[$fieldName]) || empty($_POST[$fieldName])) {
// add a reference to the errors array, indicating that these conditions have failed
$errors[$fieldName] = "The {$fieldName} is required!";
}
}
// Proceed if there aren't any errors
if (empty($errors)) {
$name = htmlspecialchars(trim($_POST['name']), ENT_QUOTES, 'UTF-8' );
$email = htmlspecialchars(trim($_POST['email']), ENT_QUOTES, 'UTF-8' );
// Email Sender Settings
$to_emails = "anonymous1#example.com, anonymous2#example.com";
$subject = 'Web Prayer Request from ' . $name;
$message = "From: {$name}";
$message .= "Email: {$email}";
$headers = "From: {$name}\r\n";
$headers .= "Reply-To: {$email}\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
if (mail($to_emails, $subject, $message, $headers)) {
echo $success_message;
} else {
echo $sendmail_error_message;
}
} else {
foreach($errors as $invalid_field_msg) {
echo "<p>{$invalid_field_msg}</p>";
}
}
}