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

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

Go to and download the latest version.

We only need 3 files from the archive:

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

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)

  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

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

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

Here you need to edit these fields for yourself:

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.

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.

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>.

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…”.

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 //):

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:

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

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

Download form

Thank you for your attention.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store