1

I am trying to get author's email in order to send a email form single.php page in WordPress. Actually I would like to get output in quotes in order to send email with wp_mail function. Right??

Here is my code for doing it all

add_action('wp_ajax_sendmail', 's3_sendmail');
add_action('wp_ajax_nopriv_sendmail', 's3_sendmail');
function s3_sendmail()
{
if(isset($_POST['submitted'])) {


if(trim($_POST['earnings']) === '') {
$earningsError = 'Please enter your earnings.';
$hasError = true;
} else {
$earnings = trim($_POST['earnings']);
}

if(trim($_POST['earnings']) === '') {
$earningsError = 'Enter an hourly rate.';
$hasError = true;
} 
else if (!preg_match("/^\d+$/", trim($_POST['earnings']))) {
$earningsError = 'Please Enter a numeric value in Estimate Earnings.';
$hasError = true;
} else {
$earnings = trim($_POST['earnings']);
}

if(trim($_POST['Duration']) === '') {
$DurationError = 'Please enter duration.';
$hasError = true;
} else {
$duration = trim($_POST['Duration']);
}

//if(trim($_POST['email']) === '') {
//$emailError = 'Please enter your email address.';
//$hasError = true;
//} 
//else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+.[a-z]{2,4}$/i", trim($_POST['email']))) {
//$emailError = 'You entered an invalid email address.';
//$hasError = true;
//} else {
//$email = trim($_POST['email']);
//}

if(trim($_POST['comments']) === '') {
$commentError = 'Please enter a description.';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}



if(!isset($hasError)) {
$emailTo = get_the_author_meta( $field, $userID );
$subject = 'email subject goes here'.$name;
$body    = "Name: $Name Duration: $duration Comments: $comments ";
$headers = "Reply-To: '".$name."' <$email> rn";
if(wp_mail( $emailTo , $subject, $body, $headers ))
{
echo "<div class='success alert alert-info'>Proposal Sent<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button></div>";
}
else
{
echo "<div class='error alert alert-danger'>Mail function Error! <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button></div></div>";
}
}
else
{
$error="<div class='error'><ul>";

if($commentError!="")
{
$error=$error.'<li>'.$commentError.'</li>';
}

if($earningsError!="")
{
$error=$error.'<li>'.$earningsError.'</li>';
}

if($DurationError!="")
{
$error=$error.'<li>'.$DurationError.'</li>';
}


$error=$error."</ul>";
echo $error;
}
}
else
{
$error="<div class='error'>Error!</div>";
}
die();
}
  • Please don't comment - just edit your question. More help on asking and such can be found in the help center and more specifically in How to Ask. Please also fix the mark up of your code for readability. – kaiser Apr 6 '14 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.