Software development, photography, jokes, ....

noyb-logo

Sites by me

 
sds82-logo
tas-logoTransportation Administration System
snoezelkussen-logo-kleinstSnoezelen Pillows for Dementia
ikzoekeenbegeleider-logoBegeleiders voor gehandicapten
Laat uw hond het jaarlijkse vuurwerk overwinnen
Betuweroute en Kunst
logo 50x50Hey Vos! Je eigen naam@vos.net emailadres?
Kunst in huis? Nicole Karrèr maakt echt bijzonder mooie dingen
nettylogo2Kunst in huis? Netty Franssen maakt ook bijzonder mooie dingen

Hosting Favorites

 
ANU Internet Services
Netim
GoDaddy
XEL Media

Blogroll

 
Bomenstichting
Google Translate
PHP
MySQL
jQuery
jQuery UI
YourHead Stacks API
Favicon Generator.
Password Generator.
Check HTTPS problems


Marc's Place


 

Install PHPMailer on MacOS X 10.11 El Capitan or higher




Buy me a coffee-1

Why use PHPMailer?


Nowadays, emails and their 'roots' are checked more and more to prevent spam. If you run your own server somewhere, like I do, and run various websites and domains for various customers, like I do, and each wants to have the ability to send emails, like I have, then you might run into problems when the mailserver is hosted elsewhere, normally with the registrar.

To illustrate this with a common example: you buy a domain name for a customer. In the DNS records, you only modify the server address for the website and you leave the mail server DNS record intact - it points to the mail server of your registrar.

Next, you send email from you server with PHP mail. The receiving mailserver checks the domain name and it leads back to your server. But the MX-records of your domain actually do not. They lead back to the servers of the company where you purchased that domain name. So there is the difference, and some mailservers accept this and others do not and see your server as a spammer.

Apple's iCloud , for example, does not care. I can send email from my server with 123.456.789.10 and it will arrive. The mailserver is actually the default mailserver of, say xel.nl or register.com . Sending the same email to a Microsoft hotmail or live account however, or some other email providers, does not work. These mailservers see the difference in IP-addresses and do not accept it.

So, to prevent being seen as a spammer, and to ensure your email arrives at its destination, you need to use the remote SMTP server of the domain, like your normal email apps.
But the default PHP mail function does not support this. And that is why some clever people created PHPMailer . 👍🏻

Check and prepare your PHP installation


1) Check your PHP installation for possible paths where PHPMailer can be installed

$ php --ini
Configuration File (php.ini) Path: /etc
Loaded Configuration File: (none)
Scan for additional .ini files in: /Library/Server/Web/Config/php
Additional .ini files parsed: (none)


2) The instructions to install PHPMailer on Unix are to copy everything to /usr/share/php.
But on Mac OS 10.11 and higher, this is not possible anymore because of Apple's new System Integrity Protection . It is not allowed to put stuff in /usr anymore, because of enhanced security of MacOS X.

Therefore, I had to check my PHP installation first, to see what, how and where.
Open Terminal and take the following steps to create a PHP script which lists which paths PHP uses to look for files to include for the require-statements:

$ cd ~/downloads
$ vi test.php


Type, or copy/paste, the following lines in the editor:

#!/usr/bin/php
<?php
echo get_include_path();
?>


Save and quit.
To be able to run this script, add the exec authorization, and then run it:

$ chmod a+x test.php
$ php test.php

.:

What you see is that my default MacOS X PHP installation does not look in any paths, except . (= current working directory).
On your Mac, it may be different, but I doubt it. If it's different, copy this path, you need it later.

3) In step (1) you saw that my PHP installation does not use a php.ini file, which I found strange. I checked which files were actually there:

$ ls /etc/php.ini*
-r--r--r-- 1 root wheel 69266  9 sep 2015 /etc/php.ini.default
-r--r--r-- 1 root wheel 69266 26 feb 2015 /etc/php.ini.default-5.2-previous
-r--r--r-- 1 root wheel 65459 24 sep 2014 /etc/php.ini.default-5.2-previous~orig


Aha, there is one. I copied that one to be the new php.ini:

$ cd /etc
$ sudo cp php.ini.default php.ini


4) The next step is to edit this file and add a common include path to be used by PHP.
The new MacOS X allows us to put stuff in /usr/local/ and I decided I want to use /usr/local/share/php:

$ sudo vi php.ini

Find the text include_path. Remove the semicolon and replace the example path by pasting the previously copied path between the double quotes.
Next, add :/usr/local/share/php to the path. My include_path looks like this:

; UNIX: "/path1:/path2"
include_path = ".:/usr/local/share/php"


Save and quit.
Create that PHP folder, if it is not there yet:

$ sudo mkdir /usr/local/share/php

5) I checked again if PHP now uses the new php.ini and the new include path.

$ php --ini
Configuration File (php.ini) Path: /etc
Loaded Configuration File: /etc/php.ini
Scan for additional .ini files in: /Library/Server/Web/Config/php
Additional .ini files parsed: (none)


$ php test.php
.:/usr/local/share/php

Download and install PHPMailer


6) Download PHPmailer as a zip file from https://github.com/PHPMailer/PHPMailer
phpmailer-github

Note : steps 7 and 8 are for PHPMailer version 5. For the new version 6, which has less files and no 'PHPMailerAutoload.php', read https://alexwebdevelop.com/phpmailer-tutorial/, under the heading 'Installing PHPMailer without Composer'.

7) Unzip and (re)move the examples folder and any other files/folders you do not need. Leave the languages folder.
For example, I moved the file extras/htmlfilter.php one level up into the PHPMailer-master folder and then deleted the extras folder.

Then in Terminal, and do not forget the slashes at the end of the paths, type the following command:

$ sudo cp -R ~/downloads/phpmailer-master/ /usr/local/share/php/

8) Check if the installation was successful by creating and running the following little PHP-script:

$ cd ~/downloads
$ vi test.php


Type, or copy/paste, the following lines of code to check if the installation works:

require 'PHPMailerAutoload.php';
$mail = new PHPMailer;


Save, quit and run it:

$ php ./test.php

If no errors occur, your installation succeeded.

Use a SMTP server with PHPMailer


9) And finally, to use a remote SMTP-server to avoid that your server IP will be listed as a spammer:

$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.xel.nl'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myuser'; // SMTP username
$mail->Password = 'mypass'; // SMTP password


Tips for using PHPMailer


When you use client-side editors as <textarea> replacements in your web-forms, like TinyMCE or CKeditor , or convert HTML < and > to &lt; and &gt; yourself, before writing the text into a database table, you might end up with these converted HTML characters in the emails you are sending out.

10) So, before you use this kind of text data, convert these characters back to normal characters first:

$maildata = htmlspecialchars_decode($mysqlresultobject->textfield, ENT_QUOTES | ENT_HTML401);

11) When you store multi-line data from a <textarea>, that data contains \n (= <LF> = Linefeed) as line endings.
For the HTML body, you have to replace \n with <br> for this text to arrive correctly:

$mail->Body = str_replace("\n", '<br> ', $maildata); // HTML email

For the plain text part, you might have to replace \n with \r\n:

$mail->AltBody = str_replace("\n", "\r\n", $maildata); // Plain text email

12) To sign your outgoing email with a logo, add an <img> tag to the end of the body:

$mail->Body .= '<br><img src="https://www.example.com/your-logo.png" alt="your-logo" height="36" /><br>';

12) And at last, to preserve special characters like ü, ç, é, ñ etc, in your emails, always start your new PHPMailer-object like this:

$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';


That's it! Happy coding!


buy me something-2
© 1997- Marc Vos (and others)   -   Privacy Statement   -    Contact Me

On this website, Google Analytics is used to track visitor statistics. These are anonymised data about the number of visitors, which pages they visit on this site, from which regions they visit, which web browsers they use, etc.. You will also see non-personalised ads via Google AdSense. Cookies from Paddle or Paypal are placed when you click on a 'Buy now!' or 'Donate!' button, and possible cookies from Disqus when you use that system to comment on one or more blogposts.
Privacy Statement