如何在Ubuntu上配置PHP邮件发送

989
2025/3/10 15:31:49
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Ubuntu上配置PHP邮件发送可以通过多种方式实现,其中最常见的是使用sendmailpostfixphpMailer。以下是使用这些方法的详细步骤:

方法一:使用Sendmail

  1. 安装Sendmail

    sudo apt update
    sudo apt install sendmail
    
  2. 配置Sendmail 编辑Sendmail配置文件:

    sudo nano /etc/mail/sendmail.cf
    

    确保以下行存在且未被注释:

    O DaemonPortOptions=Port=submission, Name=SMTP
    
  3. 重启Sendmail服务

    sudo systemctl restart sendmail
    
  4. 测试邮件发送 创建一个PHP文件来测试邮件发送:

    <?php
    $to = 'recipient@example.com';
    $subject = 'Test Email';
    $message = 'This is a test email sent from PHP using Sendmail.';
    $headers = 'From: sender@example.com' . "\r\n" .
               'Reply-To: sender@example.com' . "\r\n" .
               'X-Mailer: PHP/' . phpversion();
    
    if (mail($to, $subject, $message, $headers)) {
        echo 'Email sent successfully!';
    } else {
        echo 'Email sending failed.';
    }
    ?>
    

方法二:使用Postfix

  1. 安装Postfix

    sudo apt update
    sudo apt install postfix
    
  2. 配置Postfix 在安装过程中,选择“Internet Site”并设置系统邮件名称。 编辑Postfix的主配置文件:

    sudo nano /etc/postfix/main.cf
    

    确保以下行存在且未被注释:

    myhostname = your_hostname
    mydomain = your_domain.com
    myorigin = $mydomain
    inet_interfaces = all
    mydestination = $myhostname, localhost.$mydomain, $mydomain
    relayhost =
    
  3. 重启Postfix服务

    sudo systemctl restart postfix
    
  4. 测试邮件发送 使用与Sendmail相同的方法测试邮件发送。

方法三:使用PHPMailer

  1. 安装PHPMailer 你可以使用Composer来安装PHPMailer:

    composer require phpmailer/phpmailer
    
  2. 创建PHP文件 创建一个PHP文件来测试邮件发送:

    <?php
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    require 'vendor/autoload.php';
    
    $mail = new PHPMailer(true);
    
    try {
        //Server settings
        $mail->SMTPDebug = 2;                                      // Enable verbose debug output
        $mail->isSMTP();                                           // Send using SMTP
        $mail->Host       = 'smtp.example.com';                     // Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
        $mail->AuthType   = 'XOAUTH2';                              // OAuth2 authentication
        $mail->Port       = 587;                                    // TCP port to connect to; use 465 for `SMTPS`
        $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption; `SMTPS` also accepted
        $mail->OAuthDebug = true;                                  // Enable verbose debug output for OAuth
        $mail->OAuthToken = 'your_oauth_token';                      // OAuth 2.0 token
    
        //Recipients
        $mail->setFrom('sender@example.com', 'Mailer');
        $mail->addAddress('recipient@example.com', 'Recipient Name');     // Add a recipient
    
        // Content
        $mail->isHTML(true);                                        // Set email format to HTML
        $mail->Subject = 'Here is the subject';
        $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
    ?>
    
  3. 测试邮件发送 运行你的PHP文件来测试邮件发送。

通过以上方法,你可以在Ubuntu上配置PHP邮件发送。选择适合你需求的方法进行配置即可。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: ubuntu虚拟机安全性如何保障