問題描述
PHP mail() 中繼到郵件服務器 (PHP mail() relay to mail server)
I noticed a couple other posts around that were similar to my problem but found that they weren't specific to my scenario or I am just not understanding things very well.
My situation: I am trying to send mail from a LAMP server using the PHP mail() function. I want to relay the mail to another dedicated mail server.
Problem: It seems I am not able to send mail...sometimes. Sometimes it seems capable of sending mail to accounts outside of the domain but it fails to send any mail to accounts within the domain/network. I have seen logs often complaining about it not being able to authenticate to the domain controller...but it shouldn't have to worry about that should it?
I guess the part I am confused about is does the PHP mail() function automatically create an SMTP message to the server? Or do I have to set the php.ini settings to look at the localhost and then configure sendmail/postfix to send the message to the mail server. Also, why would it bother to authenticate with the domain controller if I only specified that the LAMP server try to connect with the mail server?
Hopefully someone can help me get this sorted out. It's been bothering me for a while now and haven't been able to find a solution. Thank you,
參考解法
方法 1:
Start using Swiftmailer (documentation) or PhpMailer, your life will be easier...
Swiftmailer example:
require_once 'lib/swift_required.php';
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john@doe.com' => 'John Doe'))
->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
->setBody('Here is the message itself');
$mailer->send($message);
PhpMailer example :
$mail = new PHPMailer(); // defaults to using php "mail()"
$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddReplyTo("name@yourdomain.com","First Last");
$mail->AddAddress("whoto@otherdomain.com", "John Doe");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
I prefer Swiftmailer, but you select you best choice ;-)
方法 2:
does the PHP mail() function automatically create an SMTP message to the server? Or do I have to set the php.ini settings to look at the localhost and then configure sendmail/postfix to send the message to the mail server
PHP's mail() function doesn't do that much by itself. On linux, it will typically connect to a local instance of sendmail or similar, using settings defined in php.ini. Check out this section from example php.ini:
[mail function]
; For Win32 only.
; SMTP = localhost
; smtp_port = 25
; For Win32 only.
;sendmail_from = me@example.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = /usr/sbin/sendmail -t -i
For windows, SMTP server at configured port is used by mail(). For linux, defined sendmail path is used.
Using other words, PHP mail() function on windows connects to a SMTP server, be it local or remote. PHP mail() on Linux cannot do the same. On Linux, the function will only use a local sendmail installation that you need to set up yourself to connect to a SMTP server. Alternative for that kind of configuration is to use PHPMailer, Swiftmailer, Zend_Mail or similar, that provide SMTP functionality by themselves.
why would it bother to authenticate with the domain controller if I only specified that the LAMP server try to connect with the mail server?
I'm far from being an expert on this, but as far as I've understood, you need to be authenticated user in your network to access outside resources which mailing would need. A domain controller gives out that kind of permissions.