【问题标题】:How to send email from PHP without SMTP server installed?如何在未安装 SMTP 服务器的情况下从 PHP 发送电子邮件?
【发布时间】:2011-06-25 05:20:17
【问题描述】:

我在专用服务器上有一个经典的 LAMP 平台(Debian、Apache2、PHP5 和 MySQL)。

我听说 PHPMailer 可以在没有安装 SMTP 的情况下发送电子邮件。 PHPMailer 是最好的选择吗?

【问题讨论】:

    标签: php email smtp debian


    【解决方案1】:

    不使用 SMTP,可以使用 PHP 邮件功能: http://php.net/manual/en/function.mail.php

    bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
    

    【讨论】:

    • 提示:请记住在将任何用户提交的信息放入标题之前验证它,这样他们在使用mail时无法将自己的信息插入标题中。
    • 答案是错误的,因为无论如何你都必须配置 smtp(mail() 无论如何都会使用它)
    【解决方案2】:

    是的,PHPMailer 是一个非常不错的选择。

    例如,如果您愿意,您可以使用 google 的免费 SMTP 服务器(就像从您的 gmail 帐户发送一样。),或者您可以跳过 smtp 部分并将其作为典型的 mail() 调用发送,但使用所有正确的标题等。它提供多部分电子邮件、附件。

    也很容易设置。

    <?php
    
    $mail = new PHPMailer(true);
    
    //Send mail using gmail
    if($send_using_gmail){
        $mail->IsSMTP(); // telling the class to use SMTP
        $mail->SMTPAuth = true; // enable SMTP authentication
        $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
        $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
        $mail->Port = 465; // set the SMTP port for the GMAIL server
        $mail->Username = "your-gmail-account@gmail.com"; // GMAIL username
        $mail->Password = "your-gmail-password"; // GMAIL password
    }
    
    //Typical mail data
    $mail->AddAddress($email, $name);
    $mail->SetFrom($email_from, $name_from);
    $mail->Subject = "My Subject";
    $mail->Body = "Mail contents";
    
    try{
        $mail->Send();
        echo "Success!";
    } catch(Exception $e){
        //Something went bad
        echo "Fail - " . $mail->ErrorInfo;
    }
    
    ?>
    

    【讨论】:

    • 我正在使用类似的自动取款机
    • 那么...这使用 SMTP 和 SSL?
    • 只是澄清一下,因为问题确实说“没有安装 SMTP”。
    • “出了点问题”不是很有帮助,使用$mail-&gt;ErrorInfo :))
    • @Kolob Canyon 答案确实回答了这个问题:“例如,如果你愿意,你可以使用谷歌的免费 SMTP 服务器(就像从你的 gmail 帐户发送一样。),或者你可以跳过 smtp 部分"...
    【解决方案3】:

    您也可以使用 phpmailer 使用默认的 php mail() 函数进行发送。

    我建议不要尝试使用 mail() 函数手动执行操作,而是使用 phpmailer 并将其配置为使用 mail()。

    我想指出,即使您自己没有使用 SMTP 连接来发送邮件,mail() 函数也将使用 SMTP 连接或服务器的 sendmail 程序来发送电子邮件,因此必须对其进行配置才能正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-12
      • 1970-01-01
      • 2013-07-22
      • 2011-03-07
      • 2013-06-23
      • 2014-08-11
      相关资源
      最近更新 更多