如何在没有安装SMTP服务器的情况下从PHP发送邮件?

dyn*_*mic 22 php email debian smtp

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

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

Mār*_*dis 22

是的,PHPMailer是一个非常好的选择.

例如,如果你愿意,你可以使用谷歌免费的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;
}

?>
Run Code Online (Sandbox Code Playgroud)

  • 只是说明一下,因为问题确实说"没有安装SMTP". (3认同)