在 2 个不同的 smtp 服务器上使用 php 邮件功能
Use php mail function with 2 different smtp servers
我有一个 PHP 应用程序,它使用邮件功能发送电子邮件。作为 sendmail 应用程序,我使用 MSMTP。
所以问题是我需要使用第一个 SMTP 服务器在一个收件箱中发送邮件,并使用第二个 SMTP 服务器将邮件发送到另一个收件箱。
例如:
如果邮件应该发送到 Gmail,它将使用 Gmail SMTP 服务器。如果邮件应该发送到另一个 SMTP 服务器的收件箱,它将使用我的 SMTP 服务器。
我考虑过使用 bash 脚本,该脚本将使用具有不同配置的 MSMTP,具体取决于 "To" 字符串。但我不太确定该怎么做。
我制作了一个bash脚本。我确信它可以更好,但至少它有效。
这应该设置为 sendmail_path.
#!/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
为什么不直接使用 pear mail 包和一个迭代器呢?在迭代器(for 循环)中使用一些 IF 语句,您可以调用任何逻辑到 include/exclude 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>");
}
}
我有一个 PHP 应用程序,它使用邮件功能发送电子邮件。作为 sendmail 应用程序,我使用 MSMTP。 所以问题是我需要使用第一个 SMTP 服务器在一个收件箱中发送邮件,并使用第二个 SMTP 服务器将邮件发送到另一个收件箱。 例如: 如果邮件应该发送到 Gmail,它将使用 Gmail SMTP 服务器。如果邮件应该发送到另一个 SMTP 服务器的收件箱,它将使用我的 SMTP 服务器。
我考虑过使用 bash 脚本,该脚本将使用具有不同配置的 MSMTP,具体取决于 "To" 字符串。但我不太确定该怎么做。
我制作了一个bash脚本。我确信它可以更好,但至少它有效。 这应该设置为 sendmail_path.
#!/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
为什么不直接使用 pear mail 包和一个迭代器呢?在迭代器(for 循环)中使用一些 IF 语句,您可以调用任何逻辑到 include/exclude 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>");
}
}