Блог Андрея

 
 

Simple and reliable PHP mailer using native mail() function

Russian version

About

Installation

Usage on product server

Usage on localhost linux ubuntu and gmail service

About

This reliable and comfortable send email class with attachment support using PHP mail() function.

I use it PHP class last ten years and I see, than it more reliable then Swift_Mailer and Symfony Mailer email tools.

It is not modest, but I will write.

In November 2019 I created Symfony 3.4 project, it use gmail service on my localhost and hosting provider

mailbox in production site.

03 January 2020 year I find, that suddenly emails stopped sending from my localhost (I use gmail account and ssmtp). There were no errors in the logs of Symphonies 3.4.

I do not edited my Symfony scripts.

I try send email from Symfony 5.0.2 project, but got error «Exception occurred while flushing email queue: Connection could not be established with host smtp.gmail.com :stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed».

I try send email use my class SimpleMail - and it was successfull.

I many used this script, on different php hostings. It realy is working (for example - one from first my commits with it class here).

Therefore I public this PHP class, I hope that will helpfull for you too.

Happy New Year and Merry Christmas.

Installation

composer require landlib/simplemail

or

git clone https://github.com/lamzin-andrey/simplemail

Usage on product server

Configure mailbox in ISP Manager on your hosting provider

Create mailbox using ISP Manager interface.

Will be sure, than you can send email from it use RoundCude or other interface.

Code example

use Landlib\SimpleMail;

//Simple email
$sender = 'yoursendmailbox@yoursite.com';
$recipient = 'yourothermailbox@gmail.com';
$mailer = new SimpleMail();
$mailer->setSubject('It test package landlib/simplemail');
$mailer->setFrom($sender, 'Your name');
$mailer->setTo($recipient);
$mailer->setBody('Hello, my friend', 'text/html', 'UTF-8');
$r = $mailer->send();
var_dump($r);

//Mail with attach
$mailer->setSubject('It test package landlib/simplemail - mail with inline attachment');
$mailer->setTextWithImages('Hello, my friend, {smile}!' . "\nI am a very satisfied person!", ['{smile}' => __DIR__ . '/smile.png']);
$r = $mailer->send();
var_dump($r);

Usage on localhost linux ubuntu and gmail service

It for linux ubuntu Desktop users.

Create gmail account and allow access for unsafe applications

On 03 01 2020 it possible on link

If link do not work, configure your ssmtp (see. Configure ssmtp) and try run example script app.php from example directory.

php app.php

You can see text like this:

ssmtp: Authorization failed (535 5.7.8  https://support.google.com/mail/?p=BadCredentials h7sm24406885lfj.29 - gsmtp)
/opt/lampp/htdocs/mh.loc/www/q/q/simplemail/example/app.php:31:
bool(false)

Goto link from message and see support page - it containts link to settings page, where you can set allow access unsafe applications.

Instal and configure ssmtp server

It for linux ubuntu Desktop users.

sudo apt-get install ssmtp

Let your gmail address will `testshop@gmail.com`.

Open file /etc/ssmtp/revaliases

sudo gedit /etc/ssmtp/revaliases

Set string

root:testshop@gmail.com:smtp.gmail.com:587

Open file /etc/ssmtp/ssmtp.conf

sudo gedit /etc/ssmtp/ssmtp.conf

Set content

root=testshop@gmail.com
mailhub=smtp.gmail.com:587
hostname=smtp.gmail.com:587
UseSTARTTLS=YES
AuthUser=testshop@gmail.com
AuthPass=***** #здесь должен быть ваш пароль, кавычки не нужны
FromLineOverride=YES

Open your php.ini file (I use XAMPP, XAMPP with FastXAMPP utilite, my php.ini location is /opt/lampp/etc/php.ini)

and add (or replace) string

sendmail_path = /usr/sbin/ssmtp -t

Restart your apache server (I use XAMPP, therefore run sudo /opt/lampp/lampp restart)

Run example script app.php (see section Create gmail account ).