$mail_to_send = new PHPMailer();
$mail_to_send->FromName = $author;
$mail_to_send->From = $email;
$mail_to_send->Subject = $subject;
$mail_to_send->Body = "Mittente: ". $author . "\n" . "\n" . "Contatto di
riferimento: " . $contatto . "\n" . "\n" . "Tipo di annuncio: " . $tipo .
"\n" . "\n". $message;
$mail_to_send->AddReplyTo($email);
//contact form destination e-mail:
$mail_to_send->AddAddress('mymail#gmail.com');
foreach ($_FILES['attachment'] as $_allegato){
if ( !$_allegato['attachment']['error'] == 4 ) { //something was send
if ( $_allegato['attachment']['error'] == 0 &&
is_uploaded_file($_allegato['attachment']['tmp_name']) )
$mail_to_send->AddAttachment($_allegato['attachment']['tmp_name'],
$_allegato['attachment']['name']);
else
wp_die('Error: there was a problem with the file upload. Try again
later.');
}
}
Why is my foreach not working? foreach ($_FILES['attachment'] as $_allegato){
I need to send multiple attachments with $mail_to_send but i'm not familiar with php $files.
Thanks.
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.
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 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";