在php裡從本地經過msmtp發送郵件

來源:互聯網
上載者:User
在php裡從本地通過msmtp發送郵件

參看http://www.absolutelytech.com/2010/07/18/howto-send-emailsusing-mail-function-from-localhost-in-php-through-msmtp-using-gmail-account-on-linux/?的步驟:

?

?

Step by step instructions:

?

  1. First of all, follow this tutorial:
    Install msmtp on your linux box and configure it to work with Gmail.

    ?

    Msmtp is highly configurable and you can easily configure it to work with any smtp server. If you wish to use any other service with msmtp, you may?read its manual?and configure it.
    Proceed to next step only if you have successfully configured and are able to send a test message.

  2. Open php.ini in your text editor.
    sudo gedit /etc/php5/apache2/php.ini

    Search for ‘sendmail_path’ and change it to look like

    sendmail_path = '/usr/bin/msmtp -t'

    Save the file and exit the text editor.
    Please note that your msmtp path may vary if you are not using Ubuntu. You may find the path to executable by:

    which msmtp

  3. Restart apache:

    ?

    sudo /etc/init.d/apache2 restart

    OR

    sudo /opt/lampp/lampp restart

  4. Everything’s done. Lets test if the mail() function is working now:
    if ( mail ( [email protected]', 'Test mail from localhost', 'Working Fine.' ) );echo 'Mail sent';elseecho 'Error. Please check error log.';

    Replace the [email protected] with your own username, save it into a php file along with php delimeters in your virtual host root folder and execute it through the browser. You should receive a mail.

    跟上述步驟大同小異。

    msmtp在ubuntu下apt-get install msmtp就可以安裝, 安裝完後, msmtp --ver 查看:

    ......

    ?

    System configuration file name: /etc/msmtprc

    User configuration file name: /root/.msmtprc

    顯然要保證有/etc/msmtprc檔案並要設定好它的配置內容。我的/etc/msmtprc內容如下:

    # Set default values for all following accounts.

    defaults


    logfile /usr/local/msmtp/msmtp.log

    # The SMTP server of the provider.

    account test


    # SMTP郵件伺服器地址

    host smtp.ym.163.com


    # 發送的郵件Email

    from [email protected]

    auth login


    # 郵件伺服器登入帳號

    user [email protected]

    # 郵件伺服器登陸密碼

    password 12345678

    # Set a default account

    account default : test


    不過,我開啟上面的測試php時,開始時出來Error. Please check error log.

    發送郵件不成功,查看apache的error_log, 找到錯誤資訊:

    msmtp: /etc/msmtprc: must be owned by you


    原來是/etc/msmtprc的許可權是777的話也不行,需要限制。 ps aux|grep httpd查看apache的進程的使用者名稱是www,

    然後:

    chown www.www /etc/msmtprc

    chmod 600 /etc/msmtprc # 一定要600而不是777 !

    搞定!

    然後在mantis裡配置?$g_phpMailer_method ? ? ? ? ? ? = PHPMAILER_METHOD_MAIL

    這樣比配置 PHPMAILER_METHOD_SMTP 發郵件要快得多,都可立即收到郵件!


    ?

?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.