Forums

Send email with PHP mail

mcevoli 11 Mar, 2011
Hello to everyone!
I'm new in this forum.

I read data from database columns with Chronoconnectivity, and for each row when I click a Send button It should send email with PHP email() function.

So in my body I have this code:
<input type="button" value="Send Email" onClick="sendMail('{email_1}','{email_2}','{email_3}');">


Where email_1,email_2,email_3 are database column that are replaced with relative emails.

In header I have:
<script language="javascript" type="text/javascript">

function sendMail(a,b,c) {
alert("<?PHP phpSendMail() ?>");
}
</script>

<?php

function phpSendMail()
{

$subject = 'subj';
$message  = 'hello';


 if (mail($to, $subject, $message)) {
   echo("<p>Message successfully sent!</p>");
  } else {
   echo("<p>Message delivery failed...</p>");
  }
}
}
?>


I'm not able to pass to phpSendMail the email addresses I have in javascript sendMail.
The javascript sendMail() get the right arguments but I'm not able to pass to phpSendMail.
Is it possible to do?

Thank you very much!

Marco.
GreyHead 11 Mar, 2011
Hi mcevoli,

I don't think this approach will work. The PHP is executed *before* the page is loaded so the values you need aren't available. And I don't think that you can send emails from JavaScript directly.

You could use a mailto: link perhaps, or use Ajax to link to a mail-sender page on the server.

Bob
mcevoli 11 Mar, 2011
Thank you for your reply!
If I use a mailto: link it works, but It uses the pc mail client and not the php mail function.
Is it possibile to modify in some way the code, and to have in each row a button to send an email to relative row emails?
GreyHead 11 Mar, 2011
Hi mcevoli,

. . . use Ajax to link to a mail-sender page on the server.


Bob
mcevoli 11 Mar, 2011
I'm not so skilled in AJAX, so I changed approach.
I removed javascript and I put in my body:

<FORM method="post" action="">
   <input type="submit" name="{data}" value="Send Mail">
</FORM>


<?php
$id = $MyRow->data;
if(isset($_POST[$id])) 
{ 
    $e1 = $MyRow->email_1;
    $e2 = $MyRow->email_2;
    $e2 = $MyRow->email_3;

$email = $e1.",".$e2.",".$e3;


$subject = 'Subj';
$message = $MyRow->comunicazione;
$headers = 'From: email';

mail($e1, $subject, $message, $headers);

 echo "Email Sent!";
}
?>


Now It works! What do you think?
Thank you.
GreyHead 11 Mar, 2011
Hi mcevoli,

Looks good - if it does what you want that's great.

Bob
This topic is locked and no more replies can be posted.