Externally hosted email and using PHP to send email
Asked Answered
A

3

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?

Allamerican answered 4/8, 2017 at 7:39 Comment(6)
is this not a partial duplicate of https://mcmap.net/q/149816/-sending-email-with-php-from-an-smtp-server as you can just use SMTP to gmail to send emails from gmail serversLockhart
@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.Allamerican
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 serverLockhart
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.Blodgett
@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.Allamerican
@ChristianGollhardt That's true, I probably should of posted this on ServerFault.Allamerican
B
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:

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.

Blodgett answered 9/8, 2017 at 13:10 Comment(6)
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!Allamerican
Don't confuse From and Sender. Take a look here. 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. @AllamericanBlodgett
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. @AllamericanBlodgett
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 [email protected] 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?Allamerican
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.Blodgett
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 [email protected] 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@Lockhart
L
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.

Lanni answered 6/8, 2017 at 9:5 Comment(4)
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.Allamerican
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.Allamerican
@Allamerican well then replyTo will be best solution.Lanni
Not really, that just sets the Reply-to address, you still have to set a From: - that is required.Allamerican
L
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://mcmap.net/q/149816/-sending-email-with-php-from-an-smtp-server

Here is a way to do it with PHP PEAR

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

$from = '<[email protected]>'; //change this to your email address
$to = '<[email protected]>'; // 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' => '[email protected]', //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.

Lockhart answered 8/8, 2017 at 13:59 Comment(4)
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.Allamerican
updated to covert it as it works differently depending on the OS your usingLockhart
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.Lockhart
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.Allamerican

© 2022 - 2024 — McMap. All rights reserved.