There was a problem loading the comments.

Using PHP mail on our Shared Hosting servers

Support Portal  »  Knowledgebase  »  Viewing Article

  Print
This article relates to sending email via the PHP mail() function - on our Shared Hosting servers.
It is important to follow the information in this article to avoid recipient email servers - and our Outbound Spam Filters, blocking delivery of the outbound email.
Note:
The principle of this article applies to sending email from any website, where the email's "From" address needs to be set correctly to avoid the email being blocked for delivery.

Two key items need to be addressed when sending email via a website.
  1. The email's "From" address must be set to an email address that the website could actually send from:
    • It should not be set as: Gmail; Yahoo; Hotmail; Outlook etc.
    • It ideally should be an address at the same domain the contact from is on: website@; contact@; sales@; info@ and so on.
    • Note: You can however set the "reply-to" header to any email address.
  2. The mailing system being used should be instructed to send as that address:
    • For the likes of PHP's mail() function, this means using the "-f" switch in the function.
    • ASP.NET and other technologies have their own methods on how to specifically set the email's "From" address.
    • Note: This is especially important when sending from a Windows server.
Where:
  • In reference to item 1:
    • The reason for this is that most recipient servers will check for a valid sending address i.e. the email's "From" address, to ensure that the email can be sent as that address (see below also, explanation on item 2).
    • For most services like Google, Yahoo etc. and other email servers, should they see an email with [their] sending address originate from a server outside of their network - or the allowed sending list (SPF), the email will be either blocked outright or quarantined into spam.
  • In reference to item 2:
    • By default, when sending email via the programming languages' built-in email function, the email's "From" address will comprised of a reference to the user/webspace and the server e.g: 010101@websiteserver.com.
    • As this is the case, you need to ensure that your website instructs the mailer function to send with a correct email address - one you can send from, as otherwise any emails sent using the email address that would be set by default will more than likely be blocked for delivery.
Below are some examples of how to correctly configure the systems to send email with a valid "From" address.


PHP Mail()

This is a simplified sample of how to send email via PHP's mail() function; it is not secured in any way, and is up to you to implement your email code in a manner which reflects best practice in relation to security and operability.

// Email Information
$email_to = "someone@sample.com";
$email_from = "website@yourwebsite.com";
$email_headers = "MIME-Version: 1.0\r\n";
$email_headers.= "From: FROM NAME <website@yourwebsite.com>" . "\r\n";
$email_headers.= "Content-Type: text/html; charset=UTF-8" . "\r\n";
$email_headers.= "Reply-To: website@yourwebsite.com" . "\r\n";
$email_subject = "Checking Delivery";
$email_body = "Checking Delivery Of Message From Website";
// Send Email
$mailerResult = @mail($email_to, "$email_subject", $email_body, $email_headers, '-f ' . $email_from); // Check For Errors
if($mailerResult) {
    echo "Mail Sent!";

} else { echo "Error Sending Email!" . "<br><br>";

print_r(error_get_last());
}

Important Note:
You need to also set the "From" address in the "additional_headers" also - in the sample above "$email_headers", as this is required for the "-f" switch to function correctly; the "From:" header and "-f" address must be the same.
You may also need to use base64_encode() on your input values:
PHP: base64_encode

For more information on the "-f" switch, please see the following:
You can also carry out a Google/Search for: "PHP mail -f switch"; ensure you place the quotes around the whole term - in the search box, as otherwise the search will not function correctly.


Wordpress

By default, all emails sent by the Wordpress website will be sent via PHP's built-in mail() function, however Wordpress does not specifically set the "From" address and is subject to the same issue as mentioned above.
The simplest method to set the "From" address correctly in a Wordpress website is to install an SMTP Plugin and use that to set the "From" address for all emails that are sent by the Wordpress website; you will need to determine (and test) which SMTP plugin suits your website requirements.

Note:

It is not necessary to use SMTP Authentication - which is typically offered by these plugins, rather you simply need to set the "From" address correctly (in the plugin settings). That being said, using an actual email mailbox for sending - where possible, can rule out a great deal of in-transit issues.

Important Note:

It may be the case that this method is not sufficient for your website and as such you will need to implement a bespoke solution in order to set the "From" address correctly for sending.


Joomla

Joomla allows you to specifically set the "From" address via the installation's Dashboard - you can typically find these settings at the following location:
  1. Log into your Joomla Dashboard.
  2. In the top menu, hover over Site and then click Global Configuration.
  3. Click the Server tab.
  4. Under Mail Settings, configure the following settings and then click Save:
    • Mailer: Set this to SMTP.
    • SMTP Authentication:
      • Set this to Yes, if you are going to also set the Username/Password items below.
      • Set this to No, if you simply want to indicate the "From" address.
    • (Optional) SMTP Security: If your SMTP server uses SSL, select this option from SMTP Security
    • (Optional) SMTP Port: Enter the port for your SMTP server - typically 25.
    • (Optional) SMTP Username: Enter the username of the email address, which generally is your full email address
    • (Optional) SMTP Password: This will usually be your email address' password.
    • (Optional) SMTP Host: Enter your Outgoing Mail Server (SMTP) hostname here.

Note:

It is not necessary to use SMTP Authentication, rather you simply need to set the "From" address correctly. That being said, using an actual email mailbox for sending - where possible, can rule out a great deal of in-transit issues.

Share via
Did you find this article useful?  

Related Articles

© Netcetera