douao3636 2012-03-18 01:54
浏览 22
已采纳

如何使用smtp设置告诉朋友脚本?

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?

  • 写回答

2条回答 默认 最新

  • duanmei2805 2012-03-18 02:15
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?