【问题标题】:PHP - PHPMailer send email with multiple SMTP hostsPHP - PHPMailer 使用多个 SMTP 主机发送电子邮件
【发布时间】:2019-08-26 17:55:27
【问题描述】:

我正在使用 PHPMailer 使用 SMTP 服务器发送电子邮件。我可以成功发送电子邮件。但是,我想用多个具有不同用户名和密码的 SMTP 服务器配置代码。如何实现这个功能。根据 PHPMailer 文档,我们可以提供备份 SMTP 服务器。但是如何配置不同的用户名和密码。

下面是代码sn-p,

$mail->SMTPDebug = 2;                                       // Enable verbose debug output
$mail->isSMTP();                                            // Set mailer to use SMTP
$mail->Host       = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth   = true;                                   // Enable SMTP authentication
$mail->Username   = 'user@example.com';                     // SMTP username
$mail->Password   = 'secret';                               // SMTP password
$mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
$mail->Port       = 587;                                    // TCP port to connect to

请帮我解决这个问题。任何帮助将不胜感激。 谢谢。

【问题讨论】:

    标签: php smtp phpmailer


    【解决方案1】:

    首先定义您的服务器,然后选择您要使用的服务器,例如:

    $servers = [
        [
            'host'     => 'server1.example.com',
            'username' => 'user 1',
            'password' => 'password1',
        ],
        [
            'host'     => 'server2.example.com',
            'username' => 'user 2',
            'password' => 'password2',
        ],
        [
            'host'     => 'server3.example.com',
            'username' => 'user 3',
            'password' => 'password3',
        ],
    ];
    
    //Pick a random server (or however you want to select a server)
    $server = $servers[array_rand($servers)];
    
    //Use the selected server values for mailing
    $mail->Host = $server['host'];
    $mail->Username = $server['username'];
    $mail->Password = $server['password'];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      相关资源
      最近更新 更多