So I have created a PHP form for my wordpress site as Contact form 7 is too heavy and slows it down drastically.
But I have the same contact form on multiple 'items' on one page, so the contact form is used more than once on one page. So when the sender clicks submit depending on how many items are on the page the form will send the same amount of emails...
Now contact form 7 doesn't do this when you submit it only sends me one email. So there is a way to do it, I just don't know as I am new to PHP.
Any help would be great. Here is my sending form code but let me know if you need anything else. cheers guys:
// Sending form to admin
if ($error == false) {
// Hook to support plugin Contact Form DB
//do_action( 'name_before_send_mail', $form_data );
$to = $name_atts['email_to'];
if ($name_atts['hide_subject'] != "true") {
$subject = "(".get_bloginfo('name').") " . $form_data['form_subject'];
} else {
$subject = get_bloginfo('name');
}
$message = $form_data['form_name'] . "\r\n\r\n" . $form_data['form_email'] . "\r\n\r\n" . $form_data['form_message'] . "\r\n\r\n" . sprintf( esc_attr__( 'IP: %s' ), name_get_the_ip() );
$headers = "Content-Type: text/plain; charset=UTF-8" . "\r\n";
$headers .= "Content-Transfer-Encoding: 8bit" . "\r\n";
$headers .= "From: ".$form_data['form_name']." <".$form_data['form_email'].">" . "\r\n";
$headers .= "Reply-To: <".$form_data['form_email'].">" . "\r\n";
if(wp_mail($to, $subject, $message, $headers) == true) {
$result = $name_atts['message_success'];
$sent = true;
} else {
$result = $name_atts['message_error'];
$fail = true;
}
}
There's the hook wpcf7_before_send_mail that you can use to check for posted datas and then send need mails.
Related
I have a WordPress theme its a blogging theme but this theme doesn't have an inbuilt function to notify subscribers (users) via email when a new post is published by a team member on the website There is one more thing is it possible to notify subscribers via email when the relevant post published to on the website.
Mean User "A" like to read about "Technology" so he just receives an email once the technology blog is posted not for any other blog.
Can you please help me I don't have experience with PHP
So please help
if ( strpos($_SERVER['HTTP_REFERER'], 'edit-question') !== false ) {
// your action or send mail goes here if the post is edited
} else {
// send mail if the post is just published
$headers = 'From: "Your Site <mail#example.com>' . "\r\n" . 'Reply-To: mail#example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "Content-Type: text/html; charset=UTF-8\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$to = 'my.mail#example.com';
$subject = 'New Post Published';
$post_title = $post->post_title;
$message = 'Hi, new post is published on your website: ' . $post_title;
wp_mail($to, $subject, $message, $headers);
}
}
add_action( 'publish_post', 'mydomain_send_mail_on_new_post', 10, 3 );
but its not working
/* Please try below code for mail send if new post published */
function notifyauthor($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post Published: ".$post->post_title."";
$message = "
Hi ".$author->display_name.",
Your post, \"".$post->post_title."\" has just been published.
View post: ".get_permalink( $post_id )."
Thanks"
;
wp_mail($author->user_email, $subject, $message);
}
add_action('publish_post', 'notifyauthor');
This question already has answers here:
How do I make a redirect in PHP?
(34 answers)
Closed 12 months ago.
I have been trying to add form submit to my web form, so that it submits data to my email using POST.
So far this is what I have and it works perfectly:
<?php
if(isset($_POST['submit'])){
$user = $_POST['user'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$to = "your-webmail here";
$subject = "Applied Data";
$txt = "Name-'.$user.',<br>Phone -'.$phone.',<br>Email-'.$email.'";
$headers = "From: your#gmail.com" . "\r\n" .
$headers .= 'Cc: your#gmail.com' . "\r\n";
$ab=(mail($to,$subject,$txt,$headers));
if($ab)
{
echo"<script>alert('SignUp successfully.')</script>";
echo "<script>window.open('index.php','_self')</script>";
}
else
{
echo"Failed";
}
}
?>
Is there a way to add a redirect to this code, so it redirects after form is submitted?
You have two options (probably more...)
Refactor your code to use AJAX to process the results of the form;
Don't display the headers until you know whether the form has been submitted / processed correctly. A redirect won't work if headers have already been sent.
As you've provided a non-AJAX problem:
<?php
if(isset($_POST['submit'])){
$user = $_POST['user'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$pageHeaders = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"; // etc.
$to = "your-webmail here";
$subject = "Applied Data";
$txt = "Name-'.$user.',<br>Phone -'.$phone.',<br>Email-'.$email.'";
$headers = "From: your#gmail.com" . "\r\n" .
$headers .= 'Cc: your#gmail.com' . "\r\n";
$ab=(mail($to,$subject,$txt,$headers));
if($ab)
{
header("Location: index.php");
die();
}
else
echo $pageHeaders; // You will need to create page headers
echo"Failed";
}
}
?>
This solution doesn't allow for an alert or message to indicate successful save. This could be achieved by redirecting to an interstitial page which can redirect after a timer. See here for an example:
use $_SESSION or $_COOKIE or a flag in your DB/in a temporary file to read & write.
for example:
if (isset($_SESSION['mail_sent']) && $_SESSION['mail_sent'] === true) {
redirect_to($target_url);
exit;
}
if(isset($_POST['submit'])) {
...
if (sending was successful) {
$_SESSION['mail_sent'] === true
}
else {
$_SESSION['mail_sent'] === false
}
}
I want to send mail (only once) using php when $cstt == 10. I want to prevent sending mail on every time I refresh the page. here is my code
if ($cstt == 10) {
echo "Great";
// SENDING EMAIL
$to = "user#example.com";
$subject = "$tracking_id_user is in transit.";
$message =' Hi user, Simple html message. Best Regards!';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Company <info#company.com>' . "\r\n";
if(mail($to,$subject,$message,$headers)){
echo "mail send";
}else{
echo "email not sent";
}
}
I did the following tasks to achieve the result.
Created a Table mail_record.
added id and mail_sent columns.
after creating the table, wrote the following code.
Connectin to database
$conn = new mysqli('localhost', 'user_id', 'password', 'databasename');
Accessing the data
$sql = "SELECT * FROM mail_record";
$query = $conn->query($sql);
$row = $query->fetch_assoc();
Accessing database to get mail_sent record.
// GETTING MAIL SENT / NOT RECORD (BLANK = NOT SENT & yes10 = Sent)
$mail_status = $row(['mail_sent']);
// CREATING THE CONDITION
if($cstt == 10 and $mail_status == ''){
// SENDING EMAIL if $cstt = 10 & email is not sent (blank)
$to = "admin#example.com, user#example.com";
$subject = "email subject";
$message ='Hi user, email text';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Web admin <admin#example.com>' . "\r\n";
if(mail($to,$subject,$message,$headers)){
// ADDING The RECORD THAT EMAIL HAS BEEN SENT FOR ($CSTT == 10)
$sql = "INSERT INTO `mail_record ` (`mail_sent`) VALUES ('yes10')";
if ($conn->query($sql) === TRUE) {
echo "Email sent and record added successfully.";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
by adding a if condition / logic we can write a code to execute mail send action only once.
now if you refresh the page the first if condition will give result FALSE because $cstt == 10 TRUE but $mail_status == '' FALSE.
using this method you can be very sure that the mail will be sent only once. no matter how many times page is being refreshed.
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.
I created a php mail function in wordpress custom page and the php mail function work well. But, when I receive the email from that wp custom page, My email inbox say showing my hosting user name and hostuser#lome.dreamhost in email head section. So, is there any way to remove the user name and hosting site address?
What I use the code in wordpress custom page are below,
<?php
if (isset($_REQUEST['email'])) {
$admin_mail = get_bloginfo('admin_email');
$header .= 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= "Email From " . $_REQUEST['email'];
$subject = "Testing Form" ;
$message .= 'Testing Form For Removing user name ';
$message .= "email request and other Request are here";
mail( $admin_mail, "Subject: $subject",$message, $header );
} else {
Here is form<form>
}
?>
this is not a valid mail header
$header .= "Email From " . $_REQUEST['email'];
try:
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" ;