-4

I trying to make a tell a friend script.

The script must send an email to the address the user specifies in the form field. containing the link of the current page in the mail body.

I tried a lot of scripts and mail classes like phpmailer, etc..

But couldnt make any of them works...

Can someone help me?

UPDATE: this is my current code

<?php
require_once 'Mail.php';

$from = "xxx@xxxx.com.";

$to = $_POST["email"];
$subject = "Pear mail test";
$body = "testing pear mail. if you are reading this, it is working.";

$host = "smtp.xxxxxx.com";

$username = "xxx@xxxx.com.";
$password = "xxxxx";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);

$smtp = Mail::factory('smtp',
  array ('host' => $host,
  'port' => '587',
    'auth' => true,
    'username' => $username,
    'password' => $password));
$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");
 }
 ?>

<form action="<?php echo $PHP_SELF?>" method="post">
<fieldset>
        <legend>Recomendar</legend>
         <label for="nome">Nome</label><input name="nome" size="40" type="text" />
         <label for="email">E-mail:</label><input name="email" size="40" type="text" />
         <input type="submit" value="Enviar" />
</fieldset>
</form>

it works, but the script is activated when loading the page (return a recipient error), how can i make the script run only when user press the submit button?

Lucas Matos
  • 1,112
  • 5
  • 25
  • 42

2 Answers2

1

Pear Mail is pretty easy to use if it's available to you

http://pear.php.net/manual/en/package.mail.mail.factory.php

$headers = array
  ( "Subject" => $subj,
    "To" => $to,
    "From" => "Your Name <{$from}>",
    "Content-Type" => "text/plain",
    "MIME-Version" => "1.0"
    );

$message = "Hello, world";

$smtp = Mail::factory
  ( "smtp",
    array
    ( "host" => "???",
      "port" => 465,
      "auth" => true,
      "username" => $from,
      "password" => $pass
     )
    );

$mail = $smtp->send($to, $headers, $message);
dldnh
  • 8,923
  • 3
  • 40
  • 52
  • thank you that worked pretty well, thanks for answering and not acted like those 6 idiots who voted my question down for no reason ... – Lucas Matos Mar 19 '12 at 16:13
  • you're welcome... can't speak for people who aren't willing to just be helpful. I don't understand it. – dldnh Mar 19 '12 at 16:22
1

Try typing

php mail

into the search box above. There's lots of good stuff already posted - for example, here and here. Pear Mail is good, as dldnh says.

Community
  • 1
  • 1
Nick
  • 5,995
  • 12
  • 54
  • 78