How to send email from PHP without SMTP server ins

2019-01-17 21:15发布

问题:

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

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

回答1:

Yes, PHPMailer is a very good choice.

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.

Pretty easy to setup too.

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

?>


回答2:

Without SMTP, you can use the PHP mail function: http://php.net/manual/en/function.mail.php

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


回答3:

You can use phpmailer to send using the default php mail() function as well.

I recommend not trying to do things manually using the mail() function, use phpmailer instead and configure it to use mail().

I'd like to point out that even though you're not using an SMTP connection to send the mails yourself, the mail() function will use either an SMTP connection or the server's sendmail program to send out the emails anyways, so that will have to be configured for it to work correctly.