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

时间:2012-03-18 01:54:30

标签: php email smtp

我试图告诉朋友的剧本。

脚本必须向用户在表单字段中指定的地址发送电子邮件。包含邮件正文中当前页面的链接。

我尝试了很多脚本和邮件类,如phpmailer等。

但是不能使它们中的任何一个都有效......

有人可以帮助我吗?

更新:这是我当前的代码

<?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>

它可以工作,但是在加载页面时会激活脚本(返回收件人错误),如何在用户按下提交按钮时才能运行脚本?

2 个答案:

答案 0 :(得分:1)

如果您可以使用Pear Mail非常容易使用

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 :(得分:1)

尝试输入

  

php mail

进入上面的搜索框。已发布了许多好东西 - 例如, herehere。正如dldnh所说,Pear Mail很好。