The sane PHPMailer instruction “Sending message and files to the mail”

Шпагин Кирилл
4 min readSep 21, 2020

Instruction to work with PHPMailer without f*cking composer, in native JavaScript and without reloading the page.

Unfortunately, PHPMailer is the only easy way to send messages to your email with attachments.

1. Download PHPMailer

Go to https://github.com/PHPMailer/PHPMailer and download the latest version.

We only need 3 files from the archive:

PHPMailer-master\src\PHPMailer.php
PHPMailer-master\src\SMTP.php
PHPMailer-master\src\Exception.php

Anything else that is stored in archive is f*cking unnecessary, you can delete it.

2. Remove all junk from files

You can skip this item. It is not required.

Files written by PHPMailer developers contain an unmeasured amount of junk consisting of only comments (why?!). The weight of these three files together with the comments is 208kb, after deleting the comments the weight is 77kb. Almost in 3 times…

I suggest deleting all comments in files with some “PHP minifier”. You can use any one, I got (first hit) http://php-minify.com.

  1. Open the PHPMailer.php file
  2. Copy the content of the file
  3. Insert in the form on the site php-minify and click “Minify”.
  4. Save the result in a new file with the same name

The procedure is repeated with all three files.
For clarity, the file Exception.php

Left file before compression, right file after trash removal

3. Move files to the project

Put these 3 files into our project. I created a folder called phpmailer specially for these files.

4. Create a configuration file

Let’s create a file send.php with the following content

Here you need to edit these fields for yourself:

// Mail creation
$title = “Mail Header”;
$body = “message";
// Settings PHPMailer
$mail->Host = ‘smtp.gmail.com’; // SMTP server
$mail->Username = ‘your_login’; // Login your email
$mail->Password = ‘password’; // Password your email
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
// Mail address itself and sender's name
$mail->setFrom('mail@gmail.com', 'Sender is name');
// Receiver of mail
$mail->addAddress('youremail@gmail.com');

Save this file send.php and place it at the root of the site.

The example shows the settings for Gmail. You can use any mail for sending. For SMTP settings for other mail services see their documentation.

5. Create an HTML form

Now put this form in the right place on your website.
Pay attention to onsubmit. The second argument should be the file name, that is send.php.

6. Let’s write Javascript code

Now you just need to place this function somewhere in your code. You can copy it to your script file, which is already attached to <head>, or paste it after the closing tag </body>.

7. END (almost)

Your form should look like this.

And in case of success (or failure) you should receive a pop-up message from your browser, saying “Message sent” or “Error…”.

8. Why the error?

This is a very common question, where I can say with full confidence: 60% of the reasons for errors is your hosting, 30% is your mail, 9% is you misspelled the data from your mail, and 1% of the reasons are you a f*cking jerk who decided to change the code, and then you start to wonder sincerely why nothing works.

To find out what the reason is, you need to comment on this line of code (remove //):

//$mail->SMTPDebug = 2

Now in your browser you need to open the console (F12) > tab “console” and send a message. You should have a new line (list) to open:

cause: Error: authentication failed: Invalid user or password! I.e. incorrectly specified mail data.

If these lines are like hieroglyphs for you and you are sure that there is no error in writing your mail data, then you send two mails: one in technical support of your hosting, the other in technical support of your mail with such content:

Hi. Trying to configure PHPMailer, but I have an error in my logs:
* HERE YOU INSERT THESE LINES FROM THE CONSOLE *

After a while, they will reply and the letters will start to leave normally.

Less secure app access and GMail

If your Gmail has two-factor authentication disabled, you must be allowed to have less secure app access: Read this

9. Download files

Download form mega.nz

Thank you for your attention.

Click on “Palms” (claps) to put likes

--

--