How to send email from PHP without SMTP server installed?

前端 未结 3 1948
南笙
南笙 2020-12-09 02:46

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

相关标签:
3条回答
  • 2020-12-09 03:28

    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.

    0 讨论(0)
  • 2020-12-09 03:39

    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 ]] )
    
    0 讨论(0)
  • 2020-12-09 03:47

    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;
    }
    
    ?>
    
    0 讨论(0)
提交回复
热议问题