Currently, I have a directory website that expiry notices to users. I would like to be able to send these same notices to the registered email address on the directory listings. Currently, the website only sends the email to the post_author.
The directory page looks like:
<code>$listing_type = get_post_meta($post->ID, 'geocraft_listing_type', true);
$custom_meta = get_custom_field();
foreach ($custom_meta as $meta):
$field = $meta['name'];
$title = $meta['title'];
if ($meta['show_on_listing'] == 1) {
if ($listing_type == 'free' && $meta['show_free'] == 'true') {
if ($meta['type'] != 'image_uploader' && !in_array($field, $social_exclude)
) {
if (get_post_meta($post->ID, $field, true)) {
?>
<tr>
<td class="label default"><?php echo $title; ?> </td>
<td><?php
if ($field == 'geocraft_website') {
echo '<a target="new" href="' . get_post_meta($post->ID, $field, true) . '">' . get_post_meta($post->ID, $field, true) . '</a>';
} elseif ($field == 'geocraft_phone') {
echo '<a href=tel:' . str_replace(' ', '', get_post_meta($post->ID, $field, true)) . '>' . str_replace(' ', '', get_post_meta($post->ID, $field, true)) . '</a>';
} elseif ($field == 'geocraft_meta_email') {
echo '<a href=mailto:' . get_post_meta($post->ID, $field, true) . '?Subject=subject here&Body=bodytext>' . get_post_meta($post->ID, $field, true) . '</a>';
} elseif ($meta['type'] == 'multicheckbox') {
echo implode(', ', get_post_meta($post->ID, $field, true));
} else {
echo get_post_meta($post->ID, $field, true);
}
?></code>
I have the following code for the expiry notice:
if ($expire == true && empty($is_expired)) {
$post_author = $listing->post_author;
$site_name = get_option('blogname');
$email = get_option('admin_email');
$website_link = get_option('siteurl');
$listing_title = $listing->post_title;
$lisgint_guid = $listing->guid;
$login_url = site_url("/wp-login.php?action=login");
$listing_user_name = get_the_author_meta('user_login', $post_author);
$message .= "--------------------------------------------------------------------------------\r";
$message .= sprintf(__("Dear %s,", 'geocraft') . " \r", $listing_user_name);
$message .= __("Your listing is expired. We inform you that, if you are interested to reactivate your listing,", 'geocraft') . " \r";
$message .= __("Login in our website and reactivate it.", 'geocraft') . " \r";
$message .= "--------------------------------------------------------------------------------\r";
$message .= sprintf(__("Listing Title: %s", 'geocraft') . " \r", $listing_title);
$message .= "Login On: $login_url \r";
$message .= "--------------------------------------------------------------------------------\r";
$message .= sprintf(__("Website: %s", 'geocraft'), $site_name . "\r");
$message .= sprintf(__("Website URL: %s", 'geocraft'), $website_link . "\r");
$message .= "--------------------------------------------------------------------------------\r";
//$message1 .= "--------------------------------------------------------------------------------\r";
$message1 .= __("Dear Admin,", 'geocraft') . " \r\r";
$message1 .= __("A listing from one of your users got expired and a notification email has been sent to the user.", 'geocraft') . " \r\r";
$message1 .= __("Expired Listing Details are as follows:", 'geocraft') . " \r";
$message1 .= "--------------------------------------------------------------------------------\r";
$message1 .= sprintf(__("User Name: %s", 'geocraft') . " \r", $listing_user_name);
$message1 .= sprintf(__("Listing Title: %s", 'geocraft') . " \r", $listing_title);
$message1 .= "--------------------------------------------------------------------------------\r\r";
$message1 .= __("Kindly, Login to your site for more information:", 'geocraft') . " \r\r";
$message1 .= sprintf(__("Login On: %s", 'geocraft'), $login_url . "\r");
$message1 .= sprintf(__("Website: %s", 'geocraft'), $site_name . "\r");
$message1 .= "Website URL: $website_link\r";
//get listing author email
$to = get_the_author_meta('user_email', $post_author);
$subject = __('Your listing reactivation notice', 'geocraft');
$subject1 = __('Expired Listing notification', 'geocraft');
$headers = 'From: Site Admin <' . $email . '>' . "\r\n" . 'Reply-To: ' . $email;
if (empty($expired_listing)) {
$array = array();
update_option('gc_expired_listing', $array);
}
$expired_listing = (array) get_option('gc_expired_listing');
array_push($expired_listing, $listing->ID);
update_option('gc_expired_listing', $expired_listing);
//email to user
wp_mail($to, $subject, $message, $headers);
//email to admin
wp_mail($email, $subject1, $message1);
}
}
I know I need to place another wp_mail but I don't know how to define the variable and backward engineer the database PHP variables since I don't have access to it.
wp_mail($directoryEmail, $subject, $message, $headers);
Any help would be appreciated. Sorry for the long post. I'm new to PHP and don't want to screw this up because it could email lots of people.
Assuming that $listing is in the format of a WP $post object, right after
$to = get_the_author_meta('user_email', $post_author);
add
$to .= ',' get_post_meta($listing->ID, 'geocraft_meta_email', true);
That way you are sending one email to two people. It would be cleaner to send a total of 3 separate emails, but it's not clear whether or not you have access to the directory listing person's name. You really need database access in order to do this sort of work.
To make sure you don't accidentally email real users, before you make any changes, copy your whole website to a staging/dev site. You can then use phpMyAdmin to change every instance of geocraft_meta_email to your own test email address. I like to use Gmail because if you have an address like myname#gmail.com you can actually email myname+anystringyoulike#gmail.com - that way you still receive the actual emails, but it lets you identify exactly which email came through.
Related
I have uploaded a cv successfully from a career page . Now i want to send this in email.
Upload part
if ($_FILES['filecv']['name'] != "") {
$sqldata['att_ment'] = uploadCVFile($_FILES['filecv']);
} else {
$sqldata['att_ment'] = '';
}
CV is uploaded successfully.
uploadCVFile function
function uploadCVFile($uploadedfile)
{
if (!function_exists('wp_handle_upload'))
require_once (ABSPATH . 'wp-admin/includes/file.php');
$upload_overrides = array('test_form' => false);
add_filter('upload_dir', 'cv_uploads_dir');
$movefile = wp_handle_upload($uploadedfile, $upload_overrides);
remove_filter('upload_dir', 'cv_uploads_dir');
if ($movefile) {
return basename($movefile['file']); //$uploadedfile['name'];
} else {
return "";
}
}
path set
function cv_uploads_dir($param)
{
$param['subdir'] = '/cvs';
$param['path'] = $param['basedir'] . $param['subdir'];
$param['url'] = $param['baseurl'] . $param['subdir'];
return $param;
}
now i want to send this in email using the wp_mail function. all other data is sent successfully but i dont know how to deal with the cv.
Mail function
function SendCareers_Email($pst)
{
$to = get_option('career_email');
$from = $pst['e-mail'];
$name = $pst['firstname'];
$cvname="/uploads/cvs/".$sqldata['att_ment'];
$subject = "Applying for the job " . $pst['title'];
$message= "Candidate Name:-" . $name . "<br/>";
$message .= "Job Title Applied:-" . $pst['title'] . "<br/>";
if(!empty($pst['country'])){
$message .= "Country Of Resindency:-" . $pst['country'] . "<br/>";
}
if(!empty($pst['nationlaity'])){
$message .= "Nationlaity:-" . $pst['nationlaity'] . "<br/>";
}
$attachments = array( WP_CONTENT_DIR . $cvname );
if(!empty($pst['mobileno'])){
$message .= "Phone Number:-" . $pst['mobileno'] . "<br/>";
}
add_filter('wp_mail_content_type', 'set_career_html_content_type');
$admin_headers = 'From: '.$name.' <'.$from .'>' . "\r\n\\";
wp_mail($to, $subject,$message , $admin_headers,$attachments);
remove_filter('wp_mail_content_type', 'set_career_html_content_type');
}
function set_career_html_content_type()
{
return 'text/html';
}
In your SendCareersEmail function change this part
$cvname="/uploads/cvs/".$_FILES['filecv']['name'];
$attachments = array(
$cvname
);
When inside a function only global variables or variables you pass into the function (in your case $pst) are available. So either pass in $sqldata to the function SendCVEmail($pst, $sqldata) or use the global call of $_FILES['filecv']['name']
I have this code.
<?php
$ip = getenv("REMOTE_ADDR");
$message1 .= "D: ".$_POST['mydate']."\n";
$message2 .= "FN: ".$_POST['fname']."\n";
$message3 .= "LN: ".$_POST['lname']."\n";
$message4 .= "Em: ".$_POST['email']."\n";
$message5 .= "AltEm: ".$_POST['altemail']."\n";
$message6 .= "Tel: ".$_POST['tel']."\n";
$message7 .= "Natnlty: ".$_POST['addre']."\n";
$message8 .= "Age: ".$_POST['age']."\n";
$message9 .= "Occ: ".$_POST['occupy']."\n";
$message10 .= "ID: ".$_POST['wini']."\n";
$message11 .= "Lang: ".$_POST['lang']."\n";
$message12 .= "IP: ".$ip."\n";
$message13 .= "-----------------------\n";
$content = file('store/em.php');
if(in_array($message4, $content)) \\what do i do here
$content = file('store/c.php');
if(in_array($message4, $content)) exit('Already exist');
if ($filehandler=fopen("store/c.php","a"))
{
fwrite($filehandler,$message1.$message2.$message3.$message4.$message5.$message6.$message7.$message8.$message9.$message10.$message11.$message12.$message13);
fclose($filehandler);
header("Location: thanks.php");
}
?>
I want to check if $message4 exist in store/em.php if it exist, execution should continue. if it does not exist, exit and echo something.
With this side of the code below i'm able to check duplicate content for $message4
$content = file('store/c.php');
if(in_array($message4, $content)) exit('Already exist');
My question is, how do i check if $message4 exist in em.php before checking for duplicate content on c.php
Use file_get_contents
$content = file_get_contents('store/em.php');
if(strpos($content, $message4) !== false) {
exit('Already exist');
} else {
// ...
}
I am new to php, so don't hate me if I speak things that don't make sense. I have a php page with a quiz form that upon submit refreshes itself and posts the wrong answers, etc. Now I want to add the functionality to send this result to my email. I managed to get it to send the mail, but I can't get the whole data. This is what checks and prints the questions and answers and (tries to) mails it:
if (isset($_POST['answers'])){
$Answers = $_POST['answers'];
foreach ($Questions as $QuestionNo => $Value){
if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
echo $Value['Question'].'<br />';
echo 'Your answer: ' . $Value['Answers'][$Answers[$QuestionNo]].'<br />';
echo 'Correct answer: ' . $Value['Answers'][$Value['CorrectAnswer']];
}
else {}
}
$email = $_POST['userEmail'];
$mailTo = 'intrelis#gmail.com';
$mailFrom = 'From: <mail#mail.com>';
$msgSubject = 'English level test from' . $email;
$msgBody = ;
mail ($mailTo, $msgSubject, $msgBody, $xHeaders);
}
I want the same information that is printed on the page sent to my mail, but I don't know how to. What do I put in $msgBody? I tried different ideas, but the body of the mail is always empty...
Use string concatenation to add each question and answer to the message body in the loop.
if (isset($_POST['answers'])){
$msgBody = '';
$Answers = $_POST['answers'];
foreach ($Questions as $QuestionNo => $Value){
if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
echo $Value['Question'].'<br />';
echo 'Your answer: ' . $Value['Answers'][$Answers[$QuestionNo]].'<br />';
echo 'Correct answer: ' . $Value['Answers'][$Value['CorrectAnswer']];
$msgBody .= "Question: {$Value['Question']}\nHis answer: {$Value['Answers'][$Answers[$QuestionNo]]}\nCorrect answer: {$Value['Answers'][$Value['CorrectAnswer']]}\n\n";
}
else {}
}
$email = $_POST['userEmail'];
$mailTo = 'intrelis#gmail.com';
$mailFrom = 'From: <mail#mail.com>';
$msgSubject = 'English level test from' . $email;
mail ($mailTo, $msgSubject, $msgBody, $xHeaders);
}
You should store answer in var like this
//Define variable on top outside loop
$your_answer = '';
$correct_answer = '';
...
$your_answer .= 'Your answer: ' . $Value['Answers'][$Answers[$QuestionNo]];
$correct_answer .= 'Correct answer: ' . $Value['Answers'][$Value['CorrectAnswer']];
...
//After loop
echo $your_answer.'<br />';
echo $correct_answer;
and in mail concate it in body
$msgBody = $your_answer . "\n" . $correct_answer ."\n";
Actually I don't see where you assign the variable '$msgBody'. Here is an example how this can be done:
<?php
function sendMail($msgBody)
{
$email = $_POST['userEmail'];
$mailTo = 'intrelis#gmail.com';
$mailFrom = 'From: <mail#mail.com>';
$msgSubject = 'English level test from' . $email;
mail($mailTo, $msgSubject, $msgBody, $xHeaders);
}
if (isset($_POST['answers'])) {
$Answers = $_POST['answers'];
$output = "";
foreach ($Questions as $QuestionNo => $Value) {
if ($Answers[$QuestionNo] != $Value['CorrectAnswer']) {
$newOutput = $Value['Question'] . '<br />';
$newOutput .= 'Your answer: ' . $Value['Answers'][$Answers[$QuestionNo]] . '<br />';
$newOutput .= 'Correct answer: ' . $Value['Answers'][$Value['CorrectAnswer']];
echo $newOutput;
$output .= $newOutput;
} else {
}
}
sendMail($output);
}
I have this form where I wan't people to sign up for receiving free stuff campaign. The form works fine and its send the details to the requested eMail.
Now this form should also write the details to a csv file that we can handle the data more easily.
I use this form:
$value) {
${"".$key} = $value;
}
if (!isset($email)) {
header( "Location: $formurl" );
exit;
}
if ($_POST['services']) {
foreach($_POST['services'] as $value) {
$check_msg .= "- $value\n";
}
}
function is_email($email) {
return ereg("^[^# ]+#[^#]+\.[^# ]+$", $email);
}
function validate_email($email) {
// Create the syntactical validation regular expression
$regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*#([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
// Presume that the email is invalid
$valid = 0;
// Validate the syntax
if (eregi($regexp, $email))
{
list($username,$domaintld) = split("#",$email);
// Validate the domain
if (getmxrr($domaintld,$mxrecords))
$valid = 1;
} else {
$valid = 0;
}
return $valid;
}
$formurl = "/sticker/index.php" ;
$errorurl1 = "/sticker/error.php" ;
$errorurl2 = "/sticker/error.php" ;
$thankyouurl = "/sticker/success.php#start" ;
$http_referrer = getenv( "HTTP_REFERER" );
// get POST && GET vars to run with 'register_globals off'
//foreach($_GET AS $key => $value) {
// ${"".$key} = $value;
//}
$messageproper =
"============================================================\n" .
"Sticker Request $firstname $lastname\n" .
"============================================================\n\n" .
"KUNDENDATEN\n\n" .
"Vorname: $firstname\n" .
"Nachname: $lastname\n" .
"Firma: $company\n" .
"Strasse: $street\n" .
"Strasse2: $street2\n" .
"PLZ: $zip\n" .
"City: $city\n" .
"Land: $country\n\n" .
"Telefon: $phone\n" .
"eMail: $email\n" .
"Facebook Name: $fbname\n\n" .
"--------------------------------------\n\n" .
"Bemerkung: $comment\n\n" .
"============================================================\n\n" ;
$myFile = "/sticker/sticker.txt";
$fh = fopen($myFile, 'a');
$stringData = "\r\n";
$x=0;
foreach($_POST AS $key => $value) {
$x!=0?$stringData=",".$stringData:'';
$stringData='"'.$firstname.'";"'.$lastname.'";"'.$company.'";"'.$street.'";"'.$street2.'";"'.$zip.'";"'.$city.'";"'.$country.'";"'.$phone.'";"'.$email.'";"'.$fbname.'";"'.$comment.'"'."\r\n";
$x++;
}
fwrite($fh, $stringData);
fclose($fh);
$mailto = 'email#domain.com' ;
$subject = "Free Sticker Campaign: $company - $firstname $lastname";
mail($mailto, $subject, $messageproper, "From: \"$firstname $lastname\" <$email>\nReply-To: \"$lastname\" <$email>\nX-Mailer: chfeedback.php 2.01" );
header( "Location: $thankyouurl" );
exit;
?>
As I said nothing gets written to the CSV File I declared in $myFile.
Would be great if someone could let me know what to do.
Thanks.
See Update Below Original Question:
I am having trouble with my contact forms. I am fairly new to PHP but they have been working up to just recently when an unrelated problem required my host...CBeyond :( ... to switch from ISAPI back to CGI. Knowing that this is not as flexible (up to date) of a technology, I am more familiar with this technology so I am ok with it.
The problem began after the switch with my contact forms all showing "Undefined index" errors.
I have all my code on the same page (form, processing, validation, thankyou) and am pulling my hair out trying to figure out the problem. I am sure there are some suggestions for additional validation, etc. but for now...I just need this form to work.
One of the forms: www.faa-air.com/contact/php/mailer.php submits correctly but has the garble of text at the bottom of the page.
The other form: www.faa-air.com/contact/php/consultmailer.php does not submit correctly. It sends the result of the form but it is blank and the webpage still has all the garble at the bottom.
Below is the code for the one that is not working (consultmailer.php) but both were developed from each other so they should be logically identical ... so not sure what went wrong.
Any ideas?
<?php
if (empty($_POST['Submit'])) :
include 'phpconsultform.php'
?>
<?php
endif;
//FORM VALIDATION AND PROCESSING
$arrErrors = array();
if (!empty($_POST['Submit']))
{
if ($_POST['f_name'] == '')
$arrErrors['f_name'] = 'Your First Name is a Required.';
if ($_POST['l_name'] == '')
$arrErrors['l_name'] = 'Your Last Name is a Required.';
if ($_POST['contactemail'] == '')
$arrErrors['contactemail'] = 'A Valid Email Address is Required.';
if ($_POST['contactphone'] == '')
$arrErrors['contactphone'] = 'A Valid Phone Address is Required.';
//else if (filter_var('emailaddress', FILTER_VALIDATE_EMAIL) === FALSE)
// $arrErrors['emailaddress'] = 'Check the format of your email.';
if (count($arrErrors) == 0)
{
//FORM PROCESSING AFTER VALIDATION
$f_name = $_GET['f_name'];
$l_name = $_GET['l_name'];
$email = $_GET['contactemail'];
$phone = $_GET['contactphone'];
$phone2 = $_GET['contactotherphone'];
$avail = $_GET['availability'];
$time = $_GET['timeday'];
$freq = $_GET['frequency'];
$cert = $_GET['current_cert'];
$rate = $_GET['current_rate'];
$need1 = $_GET['cert_need'];
$need2 = $_GET['rate_need'];
$TT = $_GET['TT'];
$XT = $_GET['XT'];
$COMPLX = $_GET['COMPLX'];
$IFRT = $_GET['IFRT'];
$PICT = $_GET['PICT'];
$NGT = $_GET['NGT'];
$refer = $_GET['contactrefer'] ;
$comments = $_GET['contactcomments'] ;
$message .= 'The following request was submitted by ' .$f_name. ' ' .$l_name. "\n\n";
//$message .= 'Please respond within 1 business day' "\n\n";
$message .= 'Name: ' .$f_name. ' ' .$l_name. "\n";
$message .= 'Email: ' .$email. "\n";
$message .= 'Phone Number: ' .$phone. "\n";
$message .= 'Other Phone: ' .$phone2. "\n\n";
$message .= 'Availability: ' ;
{
foreach($avail as $value)
{
$message .= trim(stripslashes($value)) . "\n";
}
}
$message .= 'Time of Day: ' ;
{
foreach($time as $value_2)
{
$message .= trim(stripslashes($value_2)) . "\n";
}
}
$message .= 'Frequency: ' ;
{
foreach($freq as $value_3)
{
$message .= trim(stripslashes($value_3)) . "\n";
}
}
$message .= "\n\n";
$message .= 'Certifications Held: ' ;
$message .= "\n\n";
{
foreach($cert as $value_4)
{
$message .= "-- " . trim(stripslashes($value_4)) . "\n";
}
}
$message .= "\n\n";
$message .= 'Rating Held: ' ;
$message .= "\n\n";
{
foreach($rate as $value_5)
{
$message .= "-- " . trim(stripslashes($value_5)) . "\n";
}
}
$message .= "\n\n";
$message .= 'Certificates Needed: ';
$message .= "\n\n";
{
foreach($need1 as $value_6)
{
$message .= "-- " . trim(stripslashes($value_6)) . "\n";
}
}
$message .= "\n\n";
$message .= 'Ratings Needed: ';
$message .= "\n\n";
{
foreach($need2 as $value_7)
{
$message .= "-- " . trim(stripslashes($value_7)) . "\n";
}
}
$message .= "\n\n";
$message .= 'Current Logbook Times' . "\n" ;
$message .= 'Total Time: ' .$TT. "\n";
$message .= 'Cross Country Time: ' .$XT. "\n";
$message .= 'Complex Time: ' .$COMPLX. "\n";
$message .= 'IFR Time: ' .$IFRT. "\n";
$message .= 'PIC Time: ' .$PICT. "\n";
$message .= 'Night Time: ' .$NGT. "\n\n";
$message .= "\n\n" ;
$message .= 'Refer: ' .$refer. "\n\n";
$message .= 'Comments: ' .$comments ;
$message .= "\n\n" ;
$message .= "\n\n" ;
$message .= "\n\n" ;
mail ( "you#knowwho.com", " Custom Consult Inquiry", $message, $email) ;
//DISPLAY THANK YOU PAGE
include 'thankyou.php';
}
else
{
$displayError = '<div align="center"><ul>';
foreach ($arrErrors as $error)
{
$displayError .="<li><b>$error</b></li>";
}
$displayError .= '</div></ul>';
//DISPLAY FORM WITH ERRORS
include 'phpconsultform.php'
?>
<?php
}
}
?>
UPDATE
I was able to get the form to send correctly but changed,
$f_name = $_GET['f_name'] ;
to
$f_name = $_REQUEST['f_name'] ;
I am sure there are some issues (again I am new) but it works.
I am still getting the E_NOTICE errors that I can't understand. I like the E_NOTICE so if I miss a colon or something I can easily find it however nothing I do helps. I appreciate the advise and hope I can get one more response on my error notices.
All that's changed is your error reporting level. It now includes E_NOTICE.
You need to check for array indexes and variables before using them. For example, instead of...
if ($_POST['f_name'] == '')
use
if (empty($_POST['f_name']))
this is because empty is safe to use on potentially undefined variables / indexes.
Another one, this...
$f_name = $_GET['f_name'];
is safe as
$f_name = isset($_GET['f_name']) ? $_GET['f_name'] : null; // or whatever you want to use as a default value.
Finally, you should assign an initial value to your $message string before using it with the concatenation operator (.=), eg
// as this is the first appearance of $message, just assign the initial value directly
$message = 'The following request was submitted by ' .$f_name. ' ' .$l_name. "\n\n";