Sending Emails with PHP Mail and PHP Mailer: Beginner&039s Guide

If your website or web application is based on php, you can use the php mail function to send emails. can be useful for creating custom mail forms and sending simple text-based emails.

There are generally two ways to send email with php: use the built-in php mail() function or an email sending library like phpmailer.

In this tutorial, we’ll go over how to send email using both methods and identify some common php mail problems and how to fix them.

download launch checklist from website

how to send emails using php mail() function

The first method to send email directly from a php script is by using the built-in mail() function.

To use the php send mail feature, users who host their php application or site on a local server will need to set up a sendmail program by changing the php.ini file > in your php installation folder.

If you are using a hosting server, sendmail is usually already configured. however, you must ensure that your hosting provider allows you to manually manage the mail delivery service option.

hostinger users can toggle this feature off by accessing hpanel and navigating to emails -> mail service control.

By default, the sendmail service is already enabled. however, double check to be sure.

creating a test file for php mail

After making sure that sendmail is active, we’ll create a php mail script file and place it in the public_html directory.

here’s how to do it:

  1. From hpanel, go to files -> file manager to access the hosting provider’s file manager.
  2. double-click the public_html folder and select the new file icon on the top bar. name this new file testmail.php, then press create.

New file: testmail.php

  1. Double click on testmail.php to edit it. You can use the basic PHP code below, but make sure to change its parameters accordingly. We’ll describe the script components in more detail in the next subsection.
  1. when you’re done editing, click save & close.
  1. send the email by accessing yourdomain/testmail.php from the browser. Remember to change yourdomain to the domain used when creating testmail.php.
  2. The recipient will receive the following message:

understanding php mail components

To help you understand the php mail() function, we’ll go over the components of the php script we used in the previous section:

The first two lines above enable error reporting to tell you if the php script could not be executed.

This line should contain the sender’s email address. most hosting providers prohibit adding random email addresses here as they can be used for spoofing. therefore it is better to use one with your domain name to run the script successfully.

The recipient’s email address goes here. if you want to send the message to multiple recipients, separate their email addresses with commas.

Enter the email subject line here.

Here, enter the body of your email message.

This line is commonly used to add additional headers, such as from, reply-to and cc; these additional headers must be separated with crlf (rn).

See Also:  Send Email Using ASP.Net With C

this line is used to execute the function and check if it was executed correctly.

The above message will appear when the script runs successfully. alternatively, the following message will be displayed.

Please note that although additional headers are optional, it is essential to mention the from header when sending mail. otherwise you will get a notification like:

For more information on the sendmail function and its parameters, see the official php documentation.

send html emails in php

The

php mail() function can also be used to send html-formatted emails. this format is highly customizable compared to plain text messages.

The process for sending html mail is the same, but this time you must include an html message and additional parameter headers.

here is an example of a basic script to send an email with html format:

how to use phpmailer to send emails

if you want to send multiple emails it is recommended to use an external php mail package. the native php mail() function is not suitable for large volumes of emails, as it opens and closes a simple mail transfer protocol (smtp) socket connection with each email.

There are many php mail packages to choose from, including pear mail and swift mailer. in this article, we will use phpmailer.

phpmailer is a popular mailing library that supports sending mail through the mail() function or through an smtp server. gives access to a set of functions for sending emails, simplifying the php mail configuration process.

how to install phpmailer

Installation of phpmailer is quite simple, especially if you have composer installed; many shared hosting plans include this tool.

To install phpmailer manually, connect your hosting account via ssh terminal by following these steps:

  1. download and install the puty ssh client.
  2. from your hpanel, go to advanced -> ssh login and make a note of the ssh ip, port, username and password under the ssh access information.
  3. open putty and enter your ssh information in the hostname (or ip address) and port fields. then click open.

PuTTY Configuration, highlighting the Open button

  1. Once a command window appears, type in your SSH username and password and hit Enter. Remember that PuTTY will not display the password, so don’t be confused if it doesn’t appear on the screen.
  2. Execute the cd public_html command and press Enter.
  3. Then, run the composer require phpmailer/phpmailer command and hit Enter.
  1. Please wait a moment for the installation process to finish.

PuTTY terminal window

Using PHPMailer with Hostinger SMTP

after installing phpmailer, you can start using it to send php emails.

In this section, we will show you how to send email through hostinger’s smtp server using phpmailer:

  1. create an email account by accessing the hpanel, then go to emails-> email accounts -> create a new email account.
  2. fill in the new email address and set a password. then click create. make sure you remember this information as you will use it to send mail through phpmailer.

Create email account window

  1. From the same page, go to Configuration Settings -> Manual Configuration and take note of the SMTP Host and Port.
See Also:  North Kern State Prison | Spolin Law

Email configuration settings, highlighting the outgoing server (SMTP)

  1. Access the hPanel dashboard and navigate to Files -> File Manager. Click on the public_html folder and select Add New to create a new file. Name the file testphpmailer.php and click Create.
  2. Double-click on the testphpmailer.php file, then copy and paste the code below and modify it accordingly. Make sure to replace the [email protected] with your Hostinger email address and EMAIL_ACCOUNT_PASSWORD with the password.
  1. after editing the code, click save & close. To run the script, enter yourdomain.com/testphpmailer.php in the browser.

understanding phpmailer components

To understand how phpmailer works, let’s take a look at each component of the above script.

The above line imports the phpmailer class into the global namespace.

this line includes various libraries needed by phpmailer.

this creates a new phpmailer object.

The code here is used to tell the phpmailer class to use the custom smtp settings defined in the script instead of the local mail server.

The smtpdebug command allows you to see if something is wrong with the smtp connection.

this is where the smtp server address should be specified.

set the smtp port here.

this line is used to enable smtp authentication.

specify your email address here.

here, enter your email password.

This is where you should insert the sender’s email address.

This line will let the recipient know which address to reply to.

insert recipient address here.

add email subject line here.

This line is used to read the body of an html message from an external file. the file_get_contents() function here will load the content of message.html, a local file located in the public_html directory, and include it in the message.

This line contains the body of the email message.

if you want to include attachments, include the filenames and remove the double slashes from this declaration.

This line defines what happens when the script is executed.

will display an error message with an explanation if the script fails to submit.

else extends the if statement and describes what happens if the previous condition is not met.

If the email was sent successfully, this message will appear.

create a phpmailer contact form

In addition to using phpmailer to send plain php mail, users can also use it to create a contact form, allowing their audience to communicate with them.

As with the previous php scripts, it is essential to create a new php file in the public_html folder before proceeding. name it formscript.php.

then copy and paste the following script into the newly created file and modify the information it contains as appropriate:

save your changes, then run the script from your browser.

This is what the result will look like:

If a visitor submits a message through the form, they will receive a confirmation message and the content of the form will arrive in the inbox of the email address you entered here:

To see other examples of using this mailing library, visit the official phpmailer github repository.

if you are a wordpress user, you can use contact form plugins like formidable forms, gravity forms or wpforms to create forms of contact.

See Also:  Top 15 tạo yahoo mail best

WPForms plugin

How to Troubleshoot Common PHP Mail and PHPMailer Errors

In the following sections, we’ll go over some of the most common problems that can occur when using the phpmail() or phpmailer function and how to fix them.

rejected sender address: not owned by user

This error means that the server was unable to authenticate the sender using the provided details.

To fix this, check the email address you used to send the message and make sure it matches an existing one. if it points to the wrong address, change it.

Also, make sure your sender policy framework (spf) is enabled. if you use hostinger check your spf record by going to hpanel and navigate to emails-> email accounts->dns settings -> manage email delivery. if spf record is enabled, it will show as active:

gmail could not verify that yourdomain.com sent this message

If you see this warning when testing a php mail script, the reason could be one of the following:

  • There is no spf record in the domain’s dns zone. if the record is missing, or if you are using external nameservers, add a new spf txt record manually in your hpanel or cpanel.
  • you used invalid smtp authentication details. make sure you use an email address that exists and belongs to you.

mail goes to spam folder

There are several reasons why php mail can trigger spam filters. some of the most common are:

  • misleading or spam-like topic. a couple of examples include “test” or “urgent”. be sure to set a clear intent in the subject line.
  • incorrect sender address. this may invoke security measures to filter your email to prevent phishing and hacking. scams.
  • using spam trigger words. this includes phrases like “great deal” and “this is not spam.” try changing the content of your message to see if this is the problem.
  • the mailing list does not have an unsubscribe button. make sure you include an unsubscribe button go down to avoid this topic and build reader confidence.

conclusion

The php mail() function is suitable for sending small volumes of simple text messages. meanwhile, phpmailer is a better method to send mass emails or create a contact form.

To recap, to send an email using the php mail function, create a new php file in the public_html directory and enter the mail() function. then run the script using your browser.

As for sending emails with phpmailer, you will need to install the tool, create an email account and include your smtp configuration values ​​in the phpmailer script. it is also essential to create a new php file in the public_html folder.

This tutorial contains basic syntax examples that can be used to develop a contact form or other extensions for your website.

If you have any tips, tricks or ideas, feel free to share them in the comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *