How do I specify to PHP that mail() should be sent using an external mail server?

前端 未结 6 605
盖世英雄少女心
盖世英雄少女心 2020-12-11 03:50

I have my email hosted at Rackspace Email and would like to use that as my mail server for the contact form on my website.

Looking at the php.ini file, I\'m only abl

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-11 04:09

    Since I was researching this issue and stumbled across this post and a third-party php library was not an option for me.

    As we know, php uses the sendmail command of the server by default The sendmail_path option in php.ini can be changed to override the setting to your own command with it's own arguments, etc. For example: sendmail_path = /usr/bin/unix2dos | /usr/bin/dos2unix | /usr/sbin/sendmail -t -i

    SSMTP will allow you to direct outbound emails to a mailhost from your web/php server. https://wiki.archlinux.org/index.php/SSMTP

    apt-get install ssmtp
    

    Then you can use sendmail_path = /usr/sbin/ssmtp -t to tell php to use ssmtp instead of sendmail. Be sure to restart your web server after you have made changes to php.ini

    Also ensure you have configured ssmtp and validated your SPF, DKIM, DMARC records before you make the changes to sendmail_path in php.ini

    For example gmail Mail server. /etc/ssmtp/ssmtp.conf

    # The user that gets all the mails (UID < 1000, usually the admin)
    root=postmaster@yourdomain.com
    
    # The mail server (where the mail is sent to), both port 465 or 587 should be acceptable
    # See also http://mail.google.com/support/bin/answer.py?answer=78799
    mailhub=smtp.gmail.com:587
    
    # The address where the mail appears to come from for user authentication.
    rewriteDomain=yourdomain.com
    
    # The full hostname
    hostname=FQDN.yourdomain.com
    
    # Use SSL/TLS before starting negotiation
    UseTLS=Yes
    UseSTARTTLS=Yes
    
    # Username/Password
    AuthUser=postmaster@yourdomain.com
    AuthPass=postmaster-password
    
    # Email 'From header's can override the default domain?
    FromLineOverride=yes
    

    For a stack exchange question to the same see https://unix.stackexchange.com/questions/36982/can-i-set-up-system-mail-to-use-an-external-smtp-server

    To expand on this.

    If using Google, each From: email address must be setup on the sending account as an "Account You Own" setting under accounts. Otherwise google will rewrite the headers with x-google-original-from and specify the From as the sending account instead.

提交回复
热议问题