如何在未安装 SMTP 服务器的情况下从 PHP 发送电子邮件? [英] How to send email from PHP without SMTP server installed?

查看:27
本文介绍了如何在未安装 SMTP 服务器的情况下从 PHP 发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a classic LAMP platform (Debian, Apache2, PHP5 and MySQL) on a dedicated server.

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

I heard PHPMailer can send email without having installed SMTP. Is PHPMailer the best choice for this?

推荐答案

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

Yes, PHPMailer is a very good choice.

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

For example, if you want, you can use the googles free SMTP server (it's like sending from your gmail account.), or you can just skip the smtp part and send it as a typical mail() call, but with all the correct headers etc. It offers multipart e-mails, attachments.

也很容易设置.

<?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 服务器的情况下从 PHP 发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆