• Hi

    I am getting quite frustrated with trying to setup my Google G Suite email with WordPress and my Linux server. I’ve spent a week solidly trying to get my head around how send emails from a Linux server hosting WordPress and I’m not getting anywhere. There are so many variables at play with the various applications I have installed on my Linux server, and the parameters each application has, I get one thing working but break something else! I’ve indulged so much information from online in it’s now becoming an impossible task as I can’t find out what I’m doing wrong. Once I get this working, I’m actually going to be making a guide myself and sharing it to the world because it’s ridiculous that I can find any official guides on this. I refuse to install the plugin WP Mail SMTP by WPForms when there should be an already implemented feature that can handle this.

    As a suggestion to the people who develop WordPress I would like to propose a way to set SMTP server settings in the setup stage, WordPress Dashboard and WP-Cli please.

    I have public domain I purchased with Namescheap and I have an email with Google G Suite (now Google Workspace). I will refer to my public domain as example.com throughout this post. My Linux server has the FQDN of ubuntu-server.example.com and my email is info@example.com with an alias email; webmaster@example.com.

    My Apache and PHP processes are running under the Linux user and group www-data by default. Now sure if these should be different, so please advise if they should. When I go to change the Administration Email Address on the WordPress dashboard at Settings > General Settings > Administration Email Address or submit a form with ContactForm7 on the site front-end, I can see in my mail log at /var/log/mail.log the sender is always www-data@example.com – the user the PHP process is running under on the operating system.

    All I know is that WordPress uses the wp_mail() function and relies on PHP to send mail, but through some thorough reading there is also something called PHPMailer and is included with WordPress. Is PHPMailer supposed to be able to send emails without a MTA (Mail Transfer Agent) like Sendmail or Postfix?

    I have followed numerous guides on how to use Postfix as a SMTP relay to Google and majority of them mention using address re-writing before sending it on through the relay. I have setup my Google G Suite Gmail to accept ‘Less Secure Apps’ and sent some test emails and Postfix seems to be working. The interesting thing is I have commented the entire mail section of /etc/php/7.4/fpm/php.ini so I am confused how WordPress is communicating with Postfix still. I assume there are some system hooks Apache has with PHP elsewhere?

    I partially got the address re-writing function working with Postfix to re-write everything that comes from the www-data@example.com to webmaster@example.com, but in the received email the name was showing as WordPress. In some tests after playing with some Postfix settings I even received emails showing as wordpress@example.com.

    Can someone help me out here please as this all so confusing?

Viewing 3 replies - 1 through 3 (of 3 total)
  • I’m quite dumbfounded why someone would be willing to go through the trouble of installing and configuring and administering an entire mail server, but unwilling to install a simple plugin INSTEAD to get the job done!

    Anyway, to each his own.

    As a suggestion to the people who develop WordPress I would like to propose a way to set SMTP server settings in the setup stage, WordPress Dashboard and WP-Cli please.

    This has been debated for over 5 years now (as far as I can remember)… and people have even submitted code in the past to do this. But tickets have always ended up being closed with the reason that this feature is not in line with the WordPress project’s core philosophy. See this ticket for instance (be sure to read the last closing comment): https://core.trac.wordpress.org/ticket/29069

    All I know is that WordPress uses the wp_mail() function and relies on PHP to send mail, but through some thorough reading there is also something called PHPMailer and is included with WordPress. Is PHPMailer supposed to be able to send emails without a MTA (Mail Transfer Agent) like Sendmail or Postfix?

    I personally find Google very helpful in such situations. A quick googling turned up this blog post which has this:

    The wp_mail function is implemented using the PHPMailer class to send emails. By default, there is a hard coded $phpmailer->IsMail(); function call that enables the PHPMailer class to use native PHP mail function. This is why, the wp_mail function ultimately use PHP mail functions to send emails.

    And, yes, if all you want is for WordPress to use your SMTP account to send emails, you absolutely don’t need a local mail server at all. But you’ll need to configure PHPMailer with your SMTP credentials and to use SMTP to send mails, instead of the native PHP mail function.

    Googling “WordPress SMTP no plugin” turns up loads of simple PHP code to do this.

    You can place this code in your theme’s functions.php file… but this ties the critical mailing function to your theme. Should you ever change your theme, you’ll have to remember to move the code to the new theme’s functions.php file.

    So you probably want to put the code where it will persist across different themes. And WordPress’ standard way of doing this is to put the code in a special folder called “plugins” — which is exactly what the “WP Mail SMTP by WPForms” and thousands of other plugins do 😀

    Good luck, and stay safe!

    Thread Starter willowen100

    (@willowen100)

    Hi George

    Thank you very much for your response. I have now created a PHP file inside the wp-contents/ directory that uses the phpmailer_init action. I’ve then defined the values for the PHPMailer parameters in my wp-config.php file.

    I’m quite dumbfounded why someone would be willing to go through the trouble of installing and configuring and administering an entire mail server, but unwilling to install a simple plugin INSTEAD to get the job done!

    The reason I need to setup a mail server is because I have some Linux cron jobs that I would like to log by emailing them to my webmaster email address.

    PHP is running under the www-data user account as mentioned above so Postifx sees emails coming from WordPress (processed through PHP) as www-data@exmaple.com. Obviously I don’t have a user account with that name with my email provider so it doesn’t go anywhere. This is when I need to modify to send as webmaster@example.com rather than www-data@example.com and also change the sender name to Webmaster rather than WordPress. Any ideas?

    Thread Starter willowen100

    (@willowen100)

    More specifically, why is WordPress using the operating system user that PHP is running under as the user to send email? Does WordPress and PHPMailer not have something hard coded in rather than relying on the host OS? I can confirm that the user that PHP runs under does change the email in my /var/log/mail/log log by simply changing the user and group in /etc/php/7.4/fpm/pool.d/www-conf

    
    ; Unix user/group of processes
    ; Note: The user is mandatory. If the group is not set, the default user's group
    ;       will be used.
    user = www-data
    group = www-data
    
    ...
    
    ; Set permissions for unix socket, if one is used. In Linux, read/write
    ; permissions must be set in order to allow connections from a web server. Many
    ; BSD-derived systems allow connections regardless of permissions.
    ; Default Values: user and group are set as the running user
    ;                 mode is set to 0660
    listen.owner = www-data
    listen.group = www-data
    ;listen.mode = 0660
    ; When POSIX Access Control Lists are supported you can set them using
    ; these options, value is a comma separated list of user/group names.
    ; When set, listen.owner and listen.group are ignored
    ;listen.acl_users =
    ;listen.acl_groups =
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Setting up emails with WordPress on Linux’ is closed to new replies.