I have a form on my website, that is used to reset a users password. When the button is pressed, it calls this:
if($getreq == 'true') {
//Begynder: Sender mail til brugeren
$headers = 'Content-type: text/html; charset=iso-8859-1';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers = "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers = 'FROM: <BLANK>';
$recipient = $getusermail;
$subject = "Password reset";
$message = "Password has been reset";
mail($recipient, utf8_decode($subject), utf8_decode($message), $headers);
}
Everything works fine, but I would like some sort of message like "Sending..." or "Wait while sending mail" - just to notify the user that the browser is working (see image; https://i.stack.imgur.com/nd37W.png). Is this possible? I have tried google it, but havn't been able to find it (maybe I search the wrong words). I thought that I could just 'echo' before mail(), but the mail action always comes first.
<a class="button_link_small" href="edit.php?change=user&step=edit&postid=<?php echo $getPostID?>&ResetUserPass&req=true">Reset password</a>
if($getreq == 'true') {
echo "Sending mail"
//Begynder: Sender mail til brugeren
Can someone help me with a solution, and have in mind, that I need it guided out. :-) If more code shown is needed, just say.
Dude to 'Don't Panic answer I came up with this little work around:
if($getAction == 'ResetUserPass'){?>
<script type="text/javascript">
var count = 5;
var redirect = "edit.php?change=user&step=edit&postid=<?php echo $getPostID?>&req=true";
function ResetCount(){
var timer = document.getElementById("ResetCount");
if(count > 0){
count--;
timer.innerHTML = "("+count+")";
setTimeout("ResetCount()", 1000);
}else{
window.location.href = redirect;
}
}
</script>
<?php redtext("System is working, await green infobox...<span id=\"ResetCount\"><script type=\"text/javascript\">ResetCount();</script></span>");
}
if($getreq == 'true') {
doing stuff... and..
sending mail here...
greentext("Bla bla bla, system is done.");
}
Prob not the best solution, but it works for me. :-)
Related
I want to send diifferent email to both admin and user.I dont know how to do that.
The code which i am sharing with you is sending same email to both admin and user.Please help me ..
Here is my code ;
$car = $_POST['category'];
$pick = $_POST['text1'];
$drop = $_POST['text2'];
$source = $_POST['text3'];
$email= $_POST['text4'];
$to="$email";
$subject="Web Enquiry";
$message="Hi,". "\r\n" . "\r\n" .
"You've received an email with following details, from the inquiry made at the website- mail#silvertaxi.com" ."\r\n"."\r\n".
"Car Category:"." "."$car"."\r\n".
"Source Location:"." "."$pick"."\r\n".
"Destination Location:"." "."$drop"."\r\n".
"Day and Time.:"." "."$source". "\r\n".
"Email:"." "."$email". "\r\n" ."\r\n".
"Thanks & Regards,". "\r\n" .
"Web Admin"."\r\n" ;
$headers ="From:$email\n";
$headers .= 'Cc: admin#email.com' . "\r\n";
$headers.="MIME-Version: 1.0\n";
$headers.="Content-type: text/html; charset=iso 8859-1";
if(mail($to, $subject, $message,$headers))
{
echo "Your Message has been sent." ;
} else {
echo "";
}
You should call mail() function twice; one for user and another for admin. Remove the line
$headers .= 'Cc: admin#email.com' . "\r\n";
from your code.
Then, define different messages for user and admin as you want.
$to="$email";
$subject="Web Enquiry";
$message="....." //your message to user goes here
$to_admin = "admin#email.com";
$subject_admin = "...."; //subject for mail to admin
$message_admin = "....." //your message to admin goes here
Use the mail() function twice to send different emails.
if((mail($to, $subject, $message,$headers) && mail($to_admin, $subject_admin, $message_admin, $headers))
{
echo "Your Message has been sent." ;
} else {
echo "";
}
firstly you need to check he is user or admin. If he is user you can add a condition statement for its another for admin. Like this:
if($user_role == "user"){
//mail for user
.....
}
if($user_role == "admin"){
//mail for admin
.....
}
If you have any question feel free to ask.
Can I use this part to send mails?
So if I add some php code in action.php, is this sufficient for a working email script (apart from the php code)?
This is the code, (and it's in a function don't worry this is not everything on the page)
And I know there is not a subject field, but I'll just enter the name (voornaam).
var voornaam = $('#voornaam').val();
var achternaam = $('#achternaam').val();
var telefoonnummer = $('#telefoonnummer').val();
var email = $('#email').val();
$.post('action.php',{action: "button", voornaam:voornaam, achternaam:achternaam, telefoonnummer:telefoonnummer, email:email},function(res){
$('#result').html(res);
});
document.getElementById('goed').innerHTML = 'Verstuurd!';
PHP support send mail function if the web server have correct setting,
try to research for this.
And you can use plugin like PHPmailer too.
The following is a simple example to send html email use mail function.
Just put the code into action.php will be OK.
<?php
$voornaam = $_POST['voornaam'];
$achternaam = $_POST['achternaam'];
$telefoonnummer = $_POST['telefoonnummer'];
$email = $_POST['email'];
$subject = 'Sample mail';
$headers = 'MIME-Version: 1.0' ."\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$content = '<html><header><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </header><body>';
$content .= $voornaam;
$content .= $achternaam;
$content .= $telefoonnummer;
$content .= '</body></html>';
mail($email, $subject, $content, $headers); //This method sends the mail.
echo "Your email was sent!"; // success message
?>
I'm creating a simple mail form with checkboxes, a couple of input tabs, and a text input section. It uses PHP to retrieve the information then email it to a specific email. Right now I have it emailing to my own yahoo email just for testing. When I test the site on my hosting account jacobbuller.com/testsites/peacock/contact.php the form works perfectly and forwards the email from my generic "theski" server email. But when I upload the site to the actually live hosting account for peacockautoservice.com the contact form works - it executes and sends a ?msg=1 variable in the url - but I never receive the email in my Yahoo account...
Here's the PHP I am using to send the email.
<?php ob_start();
<?php
$required_field = array('name', 'email', 'message');
foreach($required_field as $fieldname) {
if (!isset($_POST[$fieldname])) {
$errors[] = $fieldname;
}}
if (empty($errors)) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$contact = $_POST['contact'];
$phone = $_POST['phone'];
$services = IsChecked('checkboxes', 'factory');
$services .= IsChecked('checkboxes', 'timing belt');
$services .= IsChecked('checkboxes', 'brakes');
$services .= IsChecked('checkboxes', 'computerized');
$services .= IsChecked('checkboxes', 'steering and suspension');
$services .= IsChecked('checkboxes', 'heating and air');
$services .= IsChecked('checkboxes', 'electrical');
$services .= IsChecked('checkboxes', 'other');
$body = "Customer:" . $name;
$body.= "Phone Number:" . $phone;
$body.= "Contact:" . $contact;
$body.= "Services:" . $services;
$body.= "Message:" . $message;
$to = "jcbbuller#yahoo.com";
$subject = "Peacock Auto Services Inquirey";
$from = $email;
$mailed = mail($to, $subject, $body, $from) or die("Error!");
}
if($mailed) {
redirect_to("../contact.php?msg=1");
}
?>
<?php
// IsChecked FUNCTION - Detemines what checkbox are/aren't checked on form.
function IsChecked($postname, $value){
if(!empty($_POST[$postname])) {
foreach($_POST[$postname] as $job) {
if ($job == $value) {
$project = " ". $value . " ";
return $project;
}
}
}
} //END IsChecked FUNCTION
function redirect_to( $location = NULL ) {
if ($location != NULL) {
header("Location: {$location}");
exit;
}
}
?>
<?php ob_end_flush(); ?>
Please let me know if you see something wrong with the PHP above or if you know why their GoDaddy hosting account is not executing the email. I tried using their customer service network but they said that they can't help me with my code...
Your issue is mainly at server end. Mail function is working because of your check on it, if it had failed, it would have given you notification. So, mails are going definitely. If mail server is working properly at your production server, then check for SPAM folder at yahoo mail server. I would suggest you to ask your hosting provider to enable SPF and DKIM records because most of email providers requires sender authentication (if it is not a spam) and these records are helpful in it.
I can also see that your not using any headers, so I would suggest you to use extended headers to avoid providers identifying you as a spammer. I use below mentioned headers and my emails never go in spam on anyprovider, but again it depends on IP reputation of the server as well.
$headers .= "Reply-To: Awaraleo <email#domain.org>\r\n";
$headers .= "Return-Path: Awaraleo <email#domain.org>\r\n";
$headers .= "From: Awaraleo <email#domain.org>\r\n";
$headers .= "Organization: Awaraleo\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
and then use it like
mail($to,$subject,$message,$headers);
Here email#domain.org should be a valid email address created on the domain where this form is implemented.
Another authentic way is to use SMTP Authentication in coding.
This is my code . When i send an email from an id to its same id then email goes to junk . Unable to identify that what is the problem in this code . For example i send an email like this
From = abc#yahoo.com
To = abc#yahoo.com
Then it goes directly to junk .
<?php
$name=$_POST['fName'];
$yemail = $_POST['yEmail'];
$femail=$_POST['fEmail'];
$message=$_POST['message'];
$from=$yemail;
$to=$femail;
$subject="Invitation for you";
$mailBody ="<table width='628' border='0'>
<tr><td align='left' valign='middle'><p><br><br>Hello,<br><br>This email is a notification to let you know that your friend has invited you to <br>visit this link <a href=www.heed-association.org>Heed Association.</a><br><br> Your friend is using this to help people living in Pakistan by donating some money.<br><br>So your can also contribute in the areas of Health, Education, Environment and Sustainable Development<br> in the earthquake affected areas of Kashmir to improve living conditions and alleviate community distress<br><br><hr><br><br><strong>Regards<br><br>Heed Association</p></td></tr></table>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: <'.$yemail.'>' . "\r\n";
if (mail($to, $subject, $mailBody, $headers)) {
echo "<script language='javascript'>
window.location = 'index.php';
</script>";
} else {
echo "<script language='javascript'>
window.location = 'tell_a_form.php';
</script>";
}
?>
I would say check your spam filters. As silly as it sounds, add the email to the safe senders list. There doesn't appear to be anything really funky going on in your code.
It's probably an oversensitive spam filter. A lot of programs can detect if the email wasn't actually sent from the email address listed in the "from" column. Usually the way to circumvent this is to have the email come from "no-reply#yourdomain.com".
This question already has answers here:
How do you make sure email you send programmatically is not automatically marked as spam?
(24 answers)
Closed 2 years ago.
I know this problem has been addressed a few times on here. I tried following the directions for setting proper headers, I still run into problems with my emails going into the spam filter in Gmail.
If anyone can please take a look at what I've tried, I'd really appreciate it. The code below is without the headers added as explained here: http://www.velvetblues.com/web-development-blog/avoid-spam-filters-with-php-mail-emails/
Thanks in advance.
define("WEBMASTER_EMAIL", 'myName#mydomain.com');
if($post)
{
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$subject = trim($_POST['subject']);
$message = stripslashes($_POST['message']);
$error = '';
// Check name
if(!$name)
$error .= 'Name required! ';
// Check email
if(!$email)
$error .= 'E-mail required! ';
if($email && !ValidateEmail($email))
$error .= 'E-mail address is not valid! ';
// Check message
if(!$message)
$error .= "Please enter your message!";
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
echo 'OK';
}
else
echo '<div class="errormsg">'.$error.'</div>';
}
Use this code :
$to = Email;
$subject = subject ;
$body = "<div> hi hi .. </div>";
$headers = 'From: YourLogoName info#domain.com' . "\r\n" ;
$headers .='Reply-To: '. $to . "\r\n" ;
$headers .='X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
if(mail($to, $subject, $body,$headers)) {
echo('<br>'."Email Sent ;D ".'</br>');
}
else
{
echo("<p>Email Message delivery failed...</p>");
}
I think this is your issue:
"From: ".$name." <".$email.">\r\n"
since you are not gmail, hotmail or your users email provider, you cannot have "From: otherdomain.com" and then deliver the mail via "mail.yourdomain.com" - this will most likely move your mail to the spam folder.
Try
"From: YourWebsiteName <noreply#yourwebsite.com>\r\n"
."Reply-To: ".$name." <".$email.">\r\n"
instead.
ALso: your code is very unsave and a prime spam target - google "email header injection php"!
Google tends to discipline not only the websites but also service providers who used to have many users spamming across the network.
If you are signed up to one of these service providers that google recognizes as spammers this might be the reason why your php mail() messages drops in to spam box in gmail. Try to chat about this issue with your server provider.
In that case you will get a warning from google in your "spam" message:
"Why is this message in Spam? We've found that lots of messages from
home.pl are spam. Learn more"
I know this question has been asked ages ago, but I thought I'd drop a 2020 answer here so that it can potentially help new visitors.
Please note:
This answer serves as a generic answer and will require you to edit some of the details according to form inputs that you are using.
You also need to update the email address in the headers etc. to the one connected to your domain.
This solution assumes you're using Google Recaptcha. If not, then just delete the part about "Google recapthca".
This script has added security and validation that shouldn't be removed.
If you're going to use Sweet Alert then you should install it into your website/app, either via CDN or NPM.
Some Javascript to create custom Sweet Alert alerts that trigger on mail send:
// Custom SweetAlert alert that gets triggered on email send
function enquirySent() {
swal({
title: "Email sent!",
text: "Thank you for your email. We'll be in contact ASAP.",
icon: "success",
button: "Okay",
});
}
function enquiryNotSent() {
swal({
title: "Oops!",
text: "Your email was NOT sent due to an error.",
icon: "error",
button: "Okay",
});
};
The PHP script to send the mail:
<?php
if (isset($_POST['submit'])) {
// For the Google recaptcha
$curlx = curl_init();
curl_setopt($curlx, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($curlx, CURLOPT_HEADER, 0);
curl_setopt($curlx, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlx, CURLOPT_POST, 1);
$post_data = [
'secret' => 'YOUR CAPTCHA SECRET KEY',
'response' => $_POST['g-recaptcha-response']
];
curl_setopt($curlx, CURLOPT_POSTFIELDS, $post_data);
$resp = json_decode(curl_exec($curlx));
curl_close($curlx);
// Google recaptcha end
// Form details (sanitized)
$name = htmlspecialchars($_POST['name']);
$surname = htmlspecialchars($_POST['surname']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
// Mail headers and details
$email_from = 'youremail#yourdomain.com';
$email_body = "You have received a new message from the user $name $surname.\nHere is the message:\n\n".$message;
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: ".$email."\r\n";
$headers .= "Return-Path: ".$email."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;
$error = false;
// Some more input validation/sanitizing
if (!preg_match("/^[a-zA-Z ]*$/",$first_name) && $first_name!="") {
$error = true;
}
if (!preg_match("/^[a-zA-Z ]*$/",$last_name) && $last_name!="") {
$error = true;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL) && $email!="") {
$error = true;
}
function IsInjected($str) {
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if (preg_match($inject,$str)) {
return true;
} else {
return false;
}
}
if (IsInjected($visitor_email)) {
echo "Bad email value!";
exit;
}
// Sending the email
if ($error == false) {
$to = "youremail#yourdomain.com";
$subject = "Enquiry from website";
mail($to, $subject, $email_body, $headers);
// Calling the email sent / not sent alerts
echo '<script type="text/javascript">',
'enquirySent()',
'</script>';
} else {
echo '<script type="text/javascript">',
'enquiryNotSent()',
'</script>';
}
}
?>