[英]Use php mail function with 2 different smtp servers
我有一個 PHP 應用程序,它使用郵件 function 發送電子郵件。 作為 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 package 和一個迭代器呢? 在迭代器(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>");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.