1

I want to call sendEmail($email) function with it's parameters in the href tag when I click on the link I get a page wrote in it FORBIDDEN "you don't have permission to access the root folder on this server"

<?php
require 'PHPMailer-master/PHPMailerAutoload.php';
function sendEmail($email) {
$mail = new PHPMailer;
$mail->isSMTP();         
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'zebalaGP@gmail.com';                 // SMTP username
$mail->Password = '******';                           // SMTP password
$mail->SMTPSecure = 'tls';   
$mail->Port = 587;            
$mail->From = 'zebalaGP@gmail.com';
$mail->FromName = 'ana grin emoticon';
$mail->addAddress($email);     // Add a recipient
$mail->isHTML(true);          
$mail->Subject = 'blabla !';
$mail->Body = 'Salam,, <br> <b> thanks for your participation grin emoticon you are especial for us grin emoticon  ';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if (!$mail->send()) {
echo 'no';
    return false;
} else {
echo 'sent grin emoticon';
    return true;
}
}


$email='ayaradwan_93@yahoo.com';
//sendEmail($email); 
$r=' <a href= ".<?php sendEmail($email); ?>." > Send Email </a> ';
echo $r;
?>
Aya Radwan
  • 181
  • 5
  • 17
  • 1
    you cant do it like that. HTML is front end and PHP is backend. You will need JS/ AJAX – CodeGodie Jun 19 '15 at 16:38
  • actually I don't know how can I use AJEX !, would you tell me how can I use it with example :)) thank u – Aya Radwan Jun 19 '15 at 16:43
  • possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Mike Jun 19 '15 at 16:44
  • [Basics of jQuery AJAX](http://jayblanchard.net/basics_of_jquery_ajax.html) – Jay Blanchard Jun 19 '15 at 16:48

2 Answers2

1

You're mixing server-side code and client-side code. They run at completely different times in completely different contexts on (usually) completely different computers.

It's not clear exactly what your code is going to do, but it's likely that you are executing the sendEmail() function immediately when the page is requested. Then since you're not echoing any result, the a tag looks like this:

<a href="">Send Email</a>

Two things:

  1. The email has already been sent.
  2. An empty href will try to load the current page or some default directory listing or something. It definitely won't invoke server-side code.

What you're trying to do is a little more involved than you think. The simplest approach, given what you have so far, would be to move all of the email functionality to another page. Something like sendMail.php. Then the page you have here would simply link to that other one:

<a href="sendMail.php">Send Email</a>

When the user clicks that link, they'd invoke the second page which itself would call sendEmail() internally and then display a result to the user. Something like:

<?php

    // define sendEmail here like you already do

    sendEmail();

?>
<p>The email has been sent.  Thank you!</p>

So the request to send the email comes from one page, the action of actually sending it is on another.

You can make this increasingly feature-rich in various ways:

  1. Instead of a link, use a form which posts values to sendEmail.php so it can use those values when composing the email.
  2. Instead of linking to the page, have a button with some JavaScript code which creates an AJAX request to sendEmail.php to send the mail without leaving the current page.
  3. etc.

But the basic idea is that your server-side code and client-side code can't interact directly. They are separated by HTTP requests. If the client-side code wants to invoke some server-side functionality, it needs to make an HTTP request to a server-side resource (a "page" in this case) which performs that action.

David
  • 208,112
  • 36
  • 198
  • 279
  • okay ! but I want to send the email in the parameter, so the email would be send ! – Aya Radwan Jun 19 '15 at 18:00
  • @AyaRadwan: In your current code you hard-code that parameter. You can still do that. Or do you mean that you want the *user* to supply their email address? If that's the case then you'd want to use a form instead of a link. Take a look at some PHP tutorials, but basically your form would include an `input type="text"` where the user can enter an email address. – David Jun 19 '15 at 18:02
  • the emails are reteived from the database and i want to when I click on it sending email – Aya Radwan Jun 19 '15 at 18:11
  • @AyaRadwan: There's no indication of that in your code, so I can't be particularly specific. But in the design I propose what you could do is fetch the emails from the database on the first page and include them as query string parameters in the links, something like: `Send Email` Then in the second page you can get the address from `$_GET['address']`. *Be careful* not to expose a web page which blindly sends emails to anybody who requests it. Spammers/bots *love* to abuse pages like that. – David Jun 19 '15 at 18:14
  • I can't run it ,, I have the testEmail file where I write the function sendEmail($email) .. and other php class where I put the href tag this tag is not read i wrote it in php variable so the php tag read it as string – Aya Radwan Jun 19 '15 at 18:33
  • @AyaRadwan: Perhaps you can elaborate more in the question? Or if it's a new enough problem, perhaps a new question? Long comment threads are not well suited to diagnose code problems, and it's not clear what you're describing. – David Jun 19 '15 at 18:37
  • thank you I pass the data in the parameter like this $email=$object['email']; $r='Send Email'; ..... appreciate your effort ^_^ @David – Aya Radwan Jun 20 '15 at 11:00
0

What you could do is a <a> link to a php page with some GET parameters. Then, in your php page, if the GET parameters are set, you use them to send email.

It could be something like this, if you want it to be the same page.

<?php
require 'PHPMailer-master/PHPMailerAutoload.php';
function sendEmail($email) {
  $mail = new PHPMailer;
  $mail->isSMTP();         
  $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
  $mail->SMTPAuth = true;                               // Enable SMTP authentication
  $mail->Username = 'zebalaGP@gmail.com';                 // SMTP username
  $mail->Password = '******';                           // SMTP password
  $mail->SMTPSecure = 'tls';   
  $mail->Port = 587;            
  $mail->From = 'zebalaGP@gmail.com';
  $mail->FromName = 'ana grin emoticon';
  $mail->addAddress($email);     // Add a recipient
  $mail->isHTML(true);          
  $mail->Subject = 'blabla !';
  $mail->Body = 'Salam,, <br> <b> thanks for your participation grin emoticon you are especial for us grin emoticon  ';
  $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
  if (!$mail->send()) {
    echo 'no';
    return false;
  } else {
    echo 'sent grin emoticon';
    return true;
  }
}


$email='ayaradwan_93@yahoo.com';
$r=' <a href= "your_page_file_name.php?mail='.$email.'" > Send Email </a> ';

//sendEmail($email); 

if(isset($_GET['mail'])){
  sendEmail($_GET['mail']);
}else{
  echo $r;
}

?>

This may be the easiest way for you to implement what you want, still, i strongly suggest the best way to do this is by submitting a form or via AJAX.

pablito.aven
  • 1,135
  • 1
  • 11
  • 29