9

I will be using G Suite (formally Google Apps) to host the email of a site that runs off a separate host.

However, there is a contact form on the website, I haven't looked at it yet, but I assume it will use the standard mail() function.

As I understand it mail() will still use the servers mail server to send the mail, it may be a dumb question, but I assume this won't cause any spam detection issues because of this? Like, I know some servers won't accept mail if the From and/or Sender headers don't match the server it is coming from (or in some cases if the email you set in these headers doesn't exist).

So, if the mail is hosted on G Suite, and the email address that is setup in the From/Sender headers exists on G Suite this won't cause any issues correct?

Lastly, I know it's probably a better idea to use SMTP to send the mail via Google, but I may not have that choice, so I wanted to find out the answer to the above just in-case.

Edit: As per Nima's answer, is this something that can be avoided, or only with using Googles SMTP server to send with?

Brett
  • 16,869
  • 50
  • 138
  • 258
  • is this not a partial duplicate of https://stackoverflow.com/a/33506709/623150 as you can just use SMTP to gmail to send emails from gmail servers – Barkermn01 Aug 08 '17 at 13:58
  • @MartinBarker No, please read my response to your answer. My question was *not* about using SMTP; I touched on that, but the primary question was based around setting the mail headers and if doing so would cause any issues. – Brett Aug 08 '17 at 17:00
  • ok so from your feed back what the hell are you wanting answered use `sendmail` or use SMTP. Being that `sendmail` is just used as a manager app in front of SMTP they are your only options in PHP, unless you want to build your own `sendmail` compatible executable for you server – Barkermn01 Aug 09 '17 at 12:29
  • This is not a PHP problem. This is a mail server problem. It could be, that you get better answers on ServerFault. However, If you want to accept other Email Providers the Emails from your MailServer, you want to make sure, that Reverse DNS (RDNS) is probably configured and matches the sender domain. – Christian Gollhardt Aug 09 '17 at 12:37
  • @MartinBarker I think my question clearly explains what I want answered; if generally doing this wouldn't cause any issues or is there a chance of it and I would be better advised to use SMTP instead directly from Google. – Brett Aug 09 '17 at 15:46
  • @ChristianGollhardt That's true, I probably should of posted this on ServerFault. – Brett Aug 09 '17 at 15:47

3 Answers3

3

If you want it simple, then simple use SMTP.

Because of spam, multiple mail server provider are blocking mails from mail servers that have no correct RDNS (Reverse DNS) and MTA name configured.

You want to make sure that all three names are matching according to your MX Record:

  • Sender Hostname (e.g *mail@demohost.com, note that from can be what ever you want)
  • MTA-Name/HELO-Hostname (Configured in Mailserver, e.g demohost.com)
  • RDNS (Basicly it gives per IP-Adress the Hostname (e.g 42.42.42.42 -> demohost.com)

Also make sure your php.ini has the correct configuration for your Mail Server. Congrats you can now send Mails using mail(...).

As I said, it's probably most simple by just using SMTP. Assign the hard work to a hoster.

Christian Gollhardt
  • 14,865
  • 16
  • 65
  • 99
  • Ok, so you're basically saying that it's probably not a good idea to set the `From:` and/or `Sender:` headers to a mail domain that GSuite is the host of when the mail in-fact isn't coming from Googles mail servers, but if I wanted to for whatever reason then I have to do what you explained above? If so, then yes, SMTP seems *far* easier ha! – Brett Aug 09 '17 at 15:53
  • Don't confuse `From` and `Sender`. Take a look [here](https://stackoverflow.com/questions/4367358/whats-the-difference-between-sender-from-and-return-path). Basicly the sender hostname is the hostname with an MX-Record to your Mailserver. Sender can have a different domain, for example your google adress. But yeah, why maintaining your own Mail Server, if you already have one. Simple authentificate per SMTP. That is the way to go. @Brett – Christian Gollhardt Aug 09 '17 at 15:59
  • Also note, you can have a seperate E-Mail Account for sending purpose, which has the "From" of your Main E-Mail Account, if that is the reason you don't like SMTP. Since gmail is known for rewriting those headers, maybe you could use another sender slave. Normaly you have a email account at your domain seller? Alternativly instead of changing From you could also changing `Reply-To`. But this is just assuming your motivation behind your question. @Brett – Christian Gollhardt Aug 09 '17 at 16:11
  • Well I don't dislike SMTP at all, just wasn't sure if I was going to be using it in this case. I usually set the `From:` header to something like `noreply@mydonain.com` and the `Reply-to:` to the users email who submitted the form; this is because there has been instances in the past where some mail servers rejected the mail when the `From:` was set to the users email address; but perhaps that's because I didn't set the `Sender:` header? – Brett Aug 09 '17 at 16:57
  • That is most probably, because a local mailserver was used, where the domain was not probably configured to (see the 3 hostnames above). The sender would normaly be set by the mailserver. Either you configure your mail server correctly, or it will never work. There is nothing you can change on php side. – Christian Gollhardt Aug 09 '17 at 17:13
  • Just following on from this when using SMTP you can set the `From` header but make sure your sending from the mail account attached to that email address you can use Alias emails in G Suit E.G `contact@yourdomain.com` could have `no-reply@` as an alias you can setup filters to auto delete emails that come into that email address then your php can login with `contact@` details and send a `From` header of `no-reply@` – Barkermn01 Aug 10 '17 at 09:44
0

When you use GSUITE for hosting emails, it's obvious that you will be providing some domain name to GSUITE.

Now emails are marked spam and not spam based on the content as well as certificates of sending server and sending servers have different services for Transactional and Marketing Oriented emails. And GSUITE only provide transactional mail service, and transactional mails from a mail service --having valid certificates and not black listed-- lands directly into Inbox or Other Label, but Spam/Promotion.

Now GSUITE is having all correct certificates and I don't think there is any consumer oriented mail service provider, which blocks emails coming from google servers.

Other Question:- Does the From Address in E-MAIL headers matters? Upto now I have never seen from address impacting anything on receiving servers, but some consumer mail services block the usage of from address other than the account email address, just like mobile operators don't let us use someone else's caller id(Ideally). But mail service providers to businesses let you use any address as from value in e-mail headers.

Edit:- If you are still unsure about delivery of emails, you can use replyTo header with out any problem.

PS:- I have tested this myself with thousands of emails but using SendGrid servers.

Ravinder Payal
  • 2,459
  • 21
  • 38
  • Setting the `From:` header can most certainly have an effect on *some* receiving mail servers, I have seen it time and time again where sometimes people aren't getting email from their contact forms and it's because the `From:` header has been set to the users email that submitted the form, rather than an email address from the sending server; this is why it is always a good idea to always use the `Reply-To:` header for that; another issue I have seen is some servers reject mail from addresses that don't actually exist. – Brett Aug 07 '17 at 16:54
  • Well yes I know I can use `reply-to` but because as I pointed out some servers reject emails because of the `From:` and/or `Sender:` address being incorrect, I am trying to establish the best method around this if I cannot use SMTP. – Brett Aug 07 '17 at 17:41
  • @Brett well then replyTo will be best solution. – Ravinder Payal Aug 07 '17 at 17:50
  • Not really, that just sets the `Reply-to` address, you still have to set a `From:` - that is required. – Brett Aug 07 '17 at 18:31
0

http://php.net/manual/en/function.mail.php

The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).

On linux the sendmail executable is used to talk to the SMTP server configured on windows you can / could configure mail() function to use SMTP

So the best way is to use SMTP directly to send the email to Gmail to send the email.

Taken from: https://stackoverflow.com/a/33506709/623150

Here is a way to do it with PHP PEAR

// Pear Mail Library
require_once "Mail.php";

$from = '<your@mail.com>'; //change this to your email address
$to = '<someone@mail.com>'; // change to address
$subject = 'Insert subject here'; // subject of mail
$body = "Hello world! this is the content of the email"; //content of mail

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'your@gmail.com', //your gmail account
        'password' => 'snip' // your password
    ));

// Send the mail
$mail = $smtp->send($to, $headers, $body);

If you use gmail smtp remember to enable SMTP in you gmail account, under settings

On a Linux Server you can't use SMTP via the mail function.

Barkermn01
  • 6,295
  • 30
  • 71
  • Thanks for your answer, but I already know how to use SMTP. My question was about using PHP's `mail` function and/or sendmail and it it would cause any issues with gmail thinking the from/sender values seemed fishy or not. – Brett Aug 08 '17 at 16:58
  • updated to covert it as it works differently depending on the OS your using – Barkermn01 Aug 09 '17 at 12:23
  • the SMTP server is what sends the email not PHP if you don't provide headers it wont overrides them on Googles SMTP server if you do it will so just leave them missing. – Barkermn01 Aug 09 '17 at 12:31
  • I know PHP doesn't technically send the email itself, but I think you know what I'm getting at here - there is a difference getting PHP to *port* or setup your mail to be sent to the SMTP server than using SMTP directly. – Brett Aug 09 '17 at 15:29