对2个不同的smtp服务器使用php邮件功能

对2个不同的smtp服务器使用php邮件功能,php,bash,email,smtp,Php,Bash,Email,Smtp,我有一个PHP应用程序,使用邮件功能发送电子邮件。作为sendmail应用程序,我使用MSMTP。 所以问题是,我需要用第一个SMTP服务器在一个收件箱中发送邮件,用第二个SMTP服务器在另一个收件箱中发送邮件。 例如: 如果邮件应该发送到Gmail,如果它将使用Gmail SMTP服务器。如果邮件应该发送到另一个SMTP服务器的收件箱,它将使用我的SMTP服务器 我考虑过使用bash脚本,该脚本将使用MSMTP进行不同的配置,具体取决于“To”字符串。但我不太确定怎么做 我制作了一个bash脚

我有一个PHP应用程序,使用邮件功能发送电子邮件。作为sendmail应用程序,我使用MSMTP。 所以问题是,我需要用第一个SMTP服务器在一个收件箱中发送邮件,用第二个SMTP服务器在另一个收件箱中发送邮件。 例如: 如果邮件应该发送到Gmail,如果它将使用Gmail SMTP服务器。如果邮件应该发送到另一个SMTP服务器的收件箱,它将使用我的SMTP服务器


我考虑过使用bash脚本,该脚本将使用MSMTP进行不同的配置,具体取决于“To”字符串。但我不太确定怎么做

我制作了一个bash脚本。我确信它可以更好,但至少它是有效的。 这应该设置为sendmail\u路径

#!/bin/bash
read -r recipient
if [[ $recipient = *'@newcbl.ru' ]]; then
  echo -e $recipient"\n""$(</dev/stdin)" | msmtp -tC /etc/msmtprc_cbl
else
  echo -e $recipient"\n""$(</dev/stdin)" | msmtp -t
fi
#/bin/bash
read-r收件人
如果[[$recipient=*'@newcbl.ru']];然后

echo-e$recipient“\n”“$”(为什么不使用pear邮件包和迭代器?使用迭代器(for循环)中的一些IF语句,您可以调用任何逻辑来包括/排除smtp服务器

require_once "Mail.php";

$smtpServers[0] = "smtp.gmail.com";
$smtpServers[1] = "smtp.server1.com";
$smtpServers[2] = "smtp.whatever.com";

for($copies=0;$copies < 2; $i++) {
       $host = $smtpServers[2];
       if (stristr($to, 'gmail.com')) { $host = $smtpServers[0]; } 
       if (stristr($to, 'hotmail.com')) { $host = $smtpServers[1]; } 

       $username = "youremail@example.com";
       $password = "your email password";
       $port = "465";
       $to = "address_form_will_send_TO@example.com";
       $email_from = "youremail@example.com";
       $email_subject = "Subject Line Here:" ;
       $email_body = "whatever you like" ;
       $email_address = "reply-to@example.com";

       $headers = array ('From' => $email_from, 'To' => $to, 'Subject' => $email_subject, 'Reply-To' => $email_address);
       $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth'        => true, 'username' => $username, 'password' => $password));
       $mail = $smtp->send($to, $headers, $email_body);


       if (PEAR::isError($mail)) {
              echo("<p>" . $mail->getMessage() . "</p>");
       } else {
              echo("<p>Message successfully sent!</p>");
       }
}
require_once“Mail.php”;
$smtpServers[0]=“smtp.gmail.com”;
$smtpServers[1]=“smtp.server1.com”;
$smtpServers[2]=“smtp.whatever.com”;
对于($copies=0;$copies<2;$i++){
$host=$smtpServers[2];
如果(stristr($to,'gmail.com'){$host=$smtpServers[0];}
如果(stristr($to,'hotmail.com'){$host=$smtpServers[1];}
$username=”youremail@example.com";
$password=“您的电子邮件密码”;
$port=“465”;
$to=“地址”表格将发送_TO@example.com";
$email_from=”youremail@example.com";
$email_subject=“此处的主题行:”;
$email\u body=“随你喜欢”;
$email\u address=“回复-to@example.com";
$headers=array('From'=>$email\u From',To'=>$To',Subject'=>$email\u Subject',Reply To'=>$email\u address);
$smtp=Mail::工厂('smtp',数组('host'=>$host',port'=>$port,'auth'=>true,'username'=>$username,'password'=>$password));
$mail=$smtp->send($to、$headers、$email\u body);
if(PEAR::isError($mail)){
echo(“”$mail->getMessage()”

”; }否则{ echo(消息已成功发送!

); } }
使用PHPmailer库不是更容易吗?我担心在我使用的应用程序中很难用phpmail替换邮件。此外,我也没有找到使用不同smtp服务器的方法,这取决于收件人的电子邮件。