Mail problems

This is certainly interesting - I wonder how WP will move on relating to this!
Currently we rely on that function as developers and AFAIK there are no plans to force smtp in core. But reading your comment it probably would make sense to do so.

I’ll have to rethink this on new projects, seems…

In WP it’s really easy to fix this issue by adding a plugin like Easy WP SMTP – WordPress Email SMTP Plugin – WordPress plugin | WordPress.org Nederlands, which will route all of the mails sent via WP through the SMTP connection. I’m not aware of any plans by core to include SMTP support right now, but eventually I see some functionality like that merging into core, as deliverability of unauthenticated messages becomes increasingly harder.

Addendum:
I usually tend to create a separate mailbox for the forms on the website, e.g. website@mydomain.extension, to avoid the issue of having the same address as “from” and “to” address. Spam filters do not like that. Also, by using a mailbox under your own domain, you control the SPF-record of your domain, so you can avoid you message being flagged due to conflicting instructions in the SPF (which could easily happen if you use the e-mailaddress of the one filling out the form as "from).

So first of all there is a BUG in Blocs that makes the Form Handler JS fail in a 404.
This, on WP Exported Theme. Not sure the same happens in other exports.
It fails into 404 because it calls ./js/path-to-file instead of <?php echo get_template_directory_uri(); ?>/js/path-to-file.
So that needs to be fixed or a Form will never work in a WP Exported Theme of blocs when it is self-hosted.
Will report that to Norm later.

Once that is fixed (or adjusted manually in a theme) we can send easily email using the self-hosted option.
No further ado, that will work immediately on any WP Ready Server, as they all will have to enable mail(), no matter what.


Now for the challenge of SMTP, yes on WP We would just install a plugin, and this is a little more complex than I initially thought when done in pure PHP. Not to speak about static HTML sites, I would not know (when not using WP/PHP with server side logic) how exactly to add and require files in such sites, but I guess it could be done with just plain PHP as well.


That said, not using a SMTP plugin but wanting to add our own SMTP solution, is not that easy, but possible.

  1. When creating the Blocs Form, we need to choose “Custom”
  2. It then asks us to add an “Action”. This, in PHP Forms generally is the path to the file which will hold the code with our Custom Mail Method. To make it simpler, when creating the form in Blocs, simply add placeholder in that field.
  3. Then export the theme or site and find the file where the Blocs form was added. Open it in a code editor.
  4. Locate the action="placeholder" part and replace that with the path to the PHP File that will hold our Custom Mail Method. In my case, I put that file in the Theme subfolder, so the path is:
    action="<?php echo get_template_directory_uri(); ?>/includes/custom-form-function.php". In other cases that might be:
    action="/path/to/site/the-site.com/public_html/whatever-site-folder/perhaps-subfolder/custom-form-function.php". Important is that custom-form-function.php path is called. custom-form-function.php is the file where we will put our Custom Mail Method.
  5. Now open (or create if you did not already) the custom-form-function.php file which is where we will add our Custom Mail Method. Here it will depend heavily what service/API you use, which will determine what code we write.

Of course, in this file you could just use a plain old simple mail() method, but remember we wanted to use SMTP :slight_smile:
So we need to find a SMTP PHP API. I used SendPulse.
Every API is based on some larger code, in the case of SendPulse, you need to install all the files (following the same structure, name and folders) from here somewhere on your server.

I added them to my theme, under the path-to-theme/sendpulse-rest-api-php/src folder. You cold add them anywhere you like, important is that later in our custom-form-function.php file we specify the path properly.

Done that, we can implement their API in our custom-form-function.php file.
We can start with this example (see Usage and do not use composer, instead require each file separately as in the comments shown on that example code)

The code shown in that example would be put into our custom-form-function.php file.
Since we call that file with the action from our Bloc Form, it will execute that code in the file once it is called.
A mail would be sent using the example setup of the SendPulse code.

We want to modify this and include the data that was inserted in the Bloc Form instead.
To get the actual contents from the Blocs Form that was submitted, we would have to listen to $_POST (or $_GET if we chose GET in the blocs form settings…)
This will give us an Array of the form inputs submitted when completing and submitting the Blocs Form.
It might look like this:

["name_40559"]=> string(8) "John Doe" 
["email_40559"]=> string(14) "some@email.com"
["message_40559"]=> string(21) "some fancy message..." 
["optin_40559"]=> string(2) "on" 

In short, those are all fields I included in the form, and the default Blocs Checkbox “Opt In” that is added when creating a Form in Blocs.
The left side shows the keys (read: name or ID), the right side the values of the fields.

This is what the user inserted in the Blocs Form when he/she pressed “submit”.
Using that, we can then compose our email, and send it away using the SendPulse PHP API.
So instead of the hardcoded strings as seen in SendPulse’s API Example we would insert our PHP variables built with the $_POST data.

Without any further sanitisation, fancytinisation and what not (sanitisation is 100%, absolutely mandatory but I omitted it because it would go out of scope and is also very different in WP than it is in raw PHP), the completed Custom Mail Method in our custom-form-function.php file looks like this:

$path_to_api = '/path/to/site/folder/where/api/is/located';
require($path_to_api.'/sendpulse-rest-api-php/src/ApiInterface.php');
require($path_to_api.'/sendpulse-rest-api-php/src/ApiClient.php');
require($path_to_api.'/sendpulse-rest-api-php/src/Storage/TokenStorageInterface.php');
require($path_to_api.'/sendpulse-rest-api-php/src/Storage/FileStorage.php');
require($path_to_api.'/sendpulse-rest-api-php/src/Storage/SessionStorage.php');
require($path_to_api.'/sendpulse-rest-api-php/src/Storage/MemcachedStorage.php');
require($path_to_api.'/sendpulse-rest-api-php/src/Storage/MemcacheStorage.php');

use Sendpulse\RestApi\ApiClient;
use Sendpulse\RestApi\Storage\FileStorage;

// API credentials from https://login.sendpulse.com/settings/#api
define('API_USER_ID', '');//add api keys
define('API_SECRET', '');//add api keys

$SPApiClient = new ApiClient(API_USER_ID, API_SECRET, new FileStorage());

/*
 * Example: Send name, email and message from Blocs Form 
 */
$email = array(
    'text' => $_POST['message_40559'],//This is the name of the input field in Form
    'subject' => 'Attention, new mail from your site.',//Simple string
    'from' => array(
        'name' => $_POST['name_40559'],//This is the name of the input field in Form
        'email' => $_POST['email_40559'],//This is the name of the input field in Form
    ),
    'to' => array(
        array(
            'email' => 'hello@oursite.com',//Simple hardcoded receiver, which is me in this case.
        ),
    ),
);
//error_log( print_r( $SPApiClient->smtpSendMail($email), true) );//Because I am dumb and often make errors ;)
$SPApiClient->smtpSendMail($email);//instantiate smtpSendMail() to send email.

This is very rough, unsanitised example. The very least it would need some sanitisation on the $_POST data so no malicious crap can be submitted by scriptkiddies.

Using this method, you cannot submit the form with AJAX as Blocs does natively, unless you implement additional logic to do so. There will be also no validation and sanitisation as done by blocs when using the self-hosted version. No redirection logic and success message or failure message is implemented either in this example. This would all require additional work, either tailored to WP, or other types of Custom CMS/Sites/Business logic.

Finally - SMTPs (at least the one I signed up for) is not immediately 100% active, since they manually review each and every user and it takes up to 24hrs to do that.
So, it means you need to wait 24 hours after subscribing to actually use this code successfully.

I am NOT sure this helps anyone lol, but it certainly helped me and was a cool exercise to do.
Now I guess I’ll go and deploy a WP Plugin doing just that a bit easier jajaja.

@Mattheus - I guess you will be quicker by simply asking your host to enable mail() or perhaps you have a choice to move over to WP, where you could use a Plugin for all this.

And sorry if the expectations where made to have a simple way, I couldn’t find any simple(r) way than above, when we go full custom-crafted thingy…
The solution I mentioned in an earlier comment seems to work with Google SMTP but that requires Google Workspace-Accoutn, which I have no access to. And, it may not work because PHP itself does not actually allow for authentication of external SMTP (and that is why Google’s solution might work, as they offer a non-authenticated solution). Also, I think the whole points of SMTP is to make things safe and secure, so a unauthenticated SMTP to me sounds like no solution.

Perhaps someone else knows more/better ways to do this in pure PHP, I will be eager to learn to it

1 Like

Now that’s one very detailed reply, I really commend you for going so in-depth. And yes, I can really see this being very helpful for a lot of folks here.

As for the input validation, you could leave this to the built-in hooks of WordPress itself. Most input types can be sanitized by WP itself: Data Sanitization/Escaping | Theme Developer Handbook | WordPress Developer Resources

Implementing this code would obviously mean you could only test after installing the theme in WordPress itself, but this seems only a minor disadvantage if this means you can have your forms handled and sanitized by WP and sent out via SMTP.

1 Like

That’s right

This is why I did not include it (sanitisation), because in PHP it wouldn’t work at all, and I wanted to keep it possibly “skinny” to just illustrate how to send something with PHP/SMTP.

Also I assume most people will want AJAX submit, which changes the thing again a bit…

In WP we have a whole armada of actions and filters for us, you barely need to know PHP anymore - which is a reason I like it so much, but it can also be a pain sometimes (because the filters and hooks sometimes act weirdly lol)

I do actually have a project laying around for full custom Forms in WP (Terms, User, and Posts all from the front end)
But it never made it past a hardcoded version as of yet.
It’s somewhere in my github/website, need to dig it out and will keep this here in mind, adding SMTP to it would be a killer move…

Anyway, yes I hope it helps someone :slight_smile:

Have a nice easter everyone!

1 Like

HI @brechtryckaert,

With your knowledge working at a web hosting company, would you be able to assist in a small problem I have - or anyone else who has knowledge or help on what I am getting.

I create small websites for live streaming events and I do use registration by the brilliant SiteLok. LINK
And the problem I get is if anyone is using their business email which is directed through ’ Office 365 ’ then when they register - I do get the email come into the registration, buy they do not get the email back to confirm they have registered. So no one using office 365 can get emails at all from SiteLok using the PHP based email function.
I always create a dedicated email address from the URL/Host for the website being used, so this should be fine - it works with everyone else - but not office 365 email users!!! They do not even get anything in Spam etc…they do not receive anything.

I have spoke to Adrian at Vibralogix who is brilliant and said this should work - and to contact my provider. I use SIteground for all my live streams and they have also been brilliant - and said there should not be any issues. I will not move away from Siteground, as the service is great and the instant scaling on the server for live events is just amazing.

So wonder what else I can do ? and not sure if this is related to what we are talking about on this thread.

Here is a Demo of the registration page I have set up DEMO-LINK

Thanks

Actually, I would need access to their mail logs to see if the mail is getting sent correctly from the end of Siteground. If this is the case, then the problem is located at Microsoft 365 (whose spamfilter can be a veritable pain in the *ss). Since I’m a direct competitor of Siteground, I doubt they’ll give me such access.
They can check these logs for you too… For them to check this for you, you would need to give them this info:

  • From-addres
  • To-addres
  • Date and time of sending

However, I don’t know if you are active on Facebook. If you are, I would suggest going into the group https://www.facebook.com/groups/wphosting, as Hristo Pandjarov is very active there. He’s one of the folks I know at Siteground and very eager to help out, especially if you feel like you’re not getting anywhere with Siteground support.

1 Like

@AdieJAM That’s perplexing. When I set up clients on the server using Office 365 I have to set the routing to remote and update the mx settings etc, but in your case it sounds like all of this is entirely separate. Are others not using Office 365 receiving registration confirmation?

1 Like

Hi @Flashman,

Thanks for the reply. It’s happening to everyone on 365 only.
Everyone else its fine - I had an event for 600 people and around 20 could not register and there emails were through 365 when I dived into the issue deeper.

Everyone else this seems fine - try my registration demo I have linked above and see if it works for you (I hope so now !!)

I will get someone using 365 to do a test next week and I will ask siteground to get some info over to me.

It is really strange!!

I do have an account - I tend to not use it much these days as I kept going on during covid last year to message MPs and Council’s trying to get support for my main business…and when I had a lot of mental health issues last year with everything, I took facebook off my devices and took my account offline - but I am back on now (just on my Mac) and check once a week/2 weeks to check on family - so yes, I will look at thus group and message this guy you have mentioned - thank you so much.

I just ran an mx check on your demo site and I see your mail is being handled by mailspamprotection.com rather than siteground. My own web host does something similar and I wouldn’t be surprised if that was the issue. It may even be solved by setting up accurate DMARC.

1 Like

Feel free to contact him with my regards :wink:

1 Like

Will do ! thank you.

1 Like

Thanks for checking.

When I type that URL it takes me to Siteground.

I wonder if I ask siteground to not use that?

Not a clue on how to do that - would i contact SiteLok for that, or SIteground?

Actually I just checked and the email on my shared package goes through spamexperts, while the reseller server does it direct. It looks like yours is going through something run by Google.

DMARC is typically set up in the zone editor of cPanel and this can be configured in different ways, but I have it set ultra strict without problems. It looks like this, but may not be appropriate in your case:

v=DMARC1; p=reject; fo=0; adkim=r; aspf=r; pct=100; sp=reject

I would be inclined to check with siteground and see if they can verify this, along with your DKIM and SPF, allowing for your using email through an alternative provider. It might be that you need an SPF record that accounts for your server IP and the IP of the remote mail server. In cPanel you can generally check under email deliverability.

Is your email set up this way because you are using the Google equivalent of Office 365? It might be worth verifying the mail settings for are all 100% correct in the zone editor, though I think you would have other problems if something was wrong.

2 Likes

Brilliant - thanks for the info. I will do a live chat with them after looking and see what i can sort out.

I am not sure how its all set up - I just follow what they tell me and set it up.
I will check the email info now.

Thanks.

1 Like

Wow,
That is a lot of information!!! Awesome job. I need to find a quiet moment to study and test all this. Thank you very much!!

To be honest;
It would be really absolutely fantastic if someone would make a custom bric and offer it in the Blocs Store, I know I would absolutely buy it!.. :pray: :pray: :pray:

To clarify what I would like this new Bric to be (If possible):
send forms with SMTP, using the SMTP setting form the host where the website is hosted.

I solved the issue by changing to another host. Next week I will be busy moving a lot of websites to the new host.

1 Like