using core php mail() to send via gmail SMTP

前端 未结 5 938
说谎
说谎 2020-12-09 09:54

Is it possible to send mail in core php via gmail smtp without using any external class?

5条回答
  •  庸人自扰
    2020-12-09 10:44

    There's a lot of miscommunication about this. It is 100% possible to send emails using gmail via PHP's simple "mail()" command. And it is 100% easy.

    Install SSMTP:

    sudo apt-get install ssmtp
    

    Edit its settings file:

    sudo nano /etc/ssmtp/ssmtp.conf
    

    Inside, make it similar to this, but with your own credentials:

    mailhub=smtp.gmail.com:587
    AuthUser=youremail@gmail.com
    AuthPass=password
    UseSTARTTLS=YES
    
    # You can only do this if you've verified your domain with Gmail.
    # If you haven't, delete, or add a # before this
    hostname=yourwebsite.com
    
    FromLineOverride=YES
    

    Lastly, open your php.ini, and search for sendmail_path and use this value:

    sendmail_path = /usr/sbin/ssmtp -t
    

    That's it! Test it out in your PHP, with the simple 1-line mail function:

    mail('to@address.com', 'Subject', 'Message', 'From: Your name ');
    

    Update on Gmail Security

    Gmail now blocks this by default. You can still do this by visiting: http://www.google.com/settings/security/lesssecureapps

    Turn this feature ON.

提交回复
热议问题