簡體   English   中英

linux msmtp 配置從 shell 發送但從 PHP/apache 失敗

[英]linux msmtp configuration sends from shell but fails from PHP/apache

linux (fedora 20) msmtp 配置從 shell 發送但從 PHP/apache 失敗,我很難過......我的目標只是通過我的本地開發網絡服務器的 gmail smtp 發送電子郵件,以測試發送郵件的代碼的輸出

php.ini sendmail 文件讀取: sendmail_path = /usr/bin/msmtp --debug -C /etc/msmtprc --read-recipients

系統上只有一個 php.ini,用於 CLI 和位於 /etc/php.ini 的網絡服務器

/etc/msmtprc 上的權限設置為 apache:apache 600

以 root 身份執行以下命令並生成測試電子郵件:

  • php -r "mail('emily@emilytench.net', '最新測試電子郵件', '測試電子郵件正文');"
  • runuser -l apache -c '/usr/bin/msmtp --debug -C /etc/msmtprc --read-recipients < /var/www/html/test.mail' (test.mail 包括 to 和 from 行)

但是當從以下腳本調用 php 郵件函數時,apache/php 會產生錯誤:

if (mail('emily@emilytench.net', 'Newest Test Email', 'Test email body'))
print "Email successfully sent";
else
print "An error occured";

錯誤期間的日志文件讀取如下:

  • /var/log/httpd/error_log:msmtp:無法連接到 smtp.gmail.com,端口 587:權限被拒絕 msmtp:無法發送郵件(來自 /etc/msmtprc 的默認帳戶)

/etc/msmtprc 包含:

defaults
auth on
tls on
tls_trust_file /etc/pki/tls/cert.pem
account default
host smtp.gmail.com
port 587
user emily@emilytench.net
from emily@emilytench.net
password [******]
auth on
syslog on

歡迎任何指向正確方向的指針......只是試圖為localhost php郵件功能通過我的gmail smtp服務器發送電子郵件實現一個簡單的途徑 - 這不是生產服務器配置,它是我本地的apache / php web服務器發展

這么晚才回復很抱歉。 我自己也在這個問題上掙扎。 問題是配置文件的文件權限。

如果沒記錯的話,你我們要求chmod文件到0600 ,因為否則它不會工作。 並且您可能使用與您的 web-server/php.ini 不同的用戶創建了該文件。

這意味着您的網絡服務器或控制 PHP 的服務器無法讀取該文件來獲取您的電子郵件配置。

此外,如果您在~/.msmtprc下創建了配置文件,那也將不起作用。 因為當與 PHP 一起使用時,MSMTP 只使用/etc/msmtprc的全局一個

這意味着您必須在/etc/msmtprc創建您的配置,然后chown配置文件以匹配您的 webs- /etc/msmtprc用戶。

由於我在 Debian 上並且使用 NGINX,因此我必須使用chown www-data:www-data /etc/msmtprc使該文件可訪問www-data在 CentOS 上,該用戶可能是httpd所以請確保您正確設置了該用戶。

這樣做之后,我能夠使用 PHP 使用 MSMTP 發送郵件,沒有任何問題。

我遇到了從 shell 發送 MSMTP 的問題,但在 CentOS 7 上不能通過 PHP 工作。在花了一整天的時間之后,我的解決方案是……

sudo -u {apacheUser} -s which msmtp

對我來說,這輸出的是/bin/msmtp而不是user/ bin 或任何本地 bin。 一旦我使用 Apache 用戶使用的路徑更新了 PHP.ini 中的 sendmail_path ,一切正常。

最終解決方案,對我來說:

sendmail_path = /bin/msmtp -t -i

另外,也許應該注意的是,我在 php.ini 中注釋了SMTPsmtp_port

由於 mstmprc 是從 kubernetes 機密掛載的,我無法更改文件所有者。 用 passwordeval 替換密碼就成功了。

passwordeval "echo the-password"

這顯然不是最安全的方式,因此理想情況下應將 echo 替換為加密工具。

我有類似錯誤msmtp: /etc/msmtprc: must be owned by you own msmtp: /etc/msmtprc: must be owned by you with openSuse 並且更改 /etc/msmtprc 的所有者不是一個選項,因為 cron 和其他服務將它用於其他目的,並導致另一個錯誤msmtp: /etc/msmtprc: must have no more than user read/write permissions

我的解決方案是:

1) 以 root 身份創建 msmtprc 的副本

cp /etc/msmtprc /etc/msmtprc_apache
chown wwwrun:www /etc/msmtprc_apache
chmod 0600 /etc/msmtprc_apache

2)更改apache php.ini設置(搜索sendmail_path)並強制配置文件(-C選項)

sendmail_path = "/usr/bin/msmtp -C /etc/msmtprc_apache -t"

3) 在 apache php.ini 設置中注釋掉

; SMTP = localhost
; smtp_port = 25

對於簡單的測試,作為 root 切換到 wwwrun 用戶並使用 php 進行測試

sudo -u wwwrun -s
php -r "mail('test@test.com', 'PHP test', 'Test from PHP as wwwrun user');"

我看到這個問題在許多論壇中被問到,但沒有得到回答 - 甚至在一個從堆棧溢出中“抓取”內容的網站中遇到了我自己的問題 - 並為任何對此問題感到困惑的人發布了這個問題的答案。 雖然這不是問題的確切答案,但它與添加到 msmtp 的 gnome 密鑰環支持有關,因為它在沒有 shell 和 tls 的情況下運行。 無法也不願意嘗試說服代碼以一種它沒有設計的方式運行,我的解決方案是,在一些阻力下,為 smtp 中繼配置 exim。

已修復 - msmtp:無法登錄到 /var/log/msmtp:無法打開:權限被拒絕

這是為下一個遇到此問題的人准備的。

msmtp -rw-rw-rw- 1 root root 266 Jun 3 06:07 /etc/msmtprc 的系統配置文件

# mimecast
account mimecast
host smtp.mail.com
port 587
protocol smtp
from admin@company.com
auth on
user authuser@company.com
password mypassword
tls on
tls_certcheck off
logfile ~/.msmtp.log
syslog off
account default : mimecast

.#mimecast is just a section header and can be deleted
account mimecast - is a title if multiple send accounts are available or needed
account default : mimecast  - is saying this is the default account used

如果需要,每個用戶的配置文件可以與具有不同用戶 ID、密碼和來自字段的系統文件相同。 注意“.” 在 .msmtprc 之前

-rw------- 1 ubuntu ubuntu 267 Jun 3 05:50 .msmtprc

日志文件是在每個用戶的主目錄中以正確的權限創建的 - 無需弄亂權限。

-rw-r--r-- 1 根 msmtp 344 Jun 3 06:09 .msmtp.log

從命令行發送電子郵件

echo -e "Subject: MySubject\r\n\r\nThis is mybody" |msmtp recipient@company.com

use the -C configfilename  to specify alternate local config files
use the -a account mimecast to switch between accounts to send from within the config file  ( did not try this option )

或使用

msmtp recipient@company.com
Subject: This is my subjectline
Blank line ( press enter )
Here is the body of the email

CTRL-D ( to send )

或使用此選項從命令行發送郵件

msmtp recipient@company.com < filename

其中文件名包含

To: recipient@company.com
From: sender@company.com
Subject: Here is the Subject

body body body .....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM