A simple example on how to send email messages with SMTP using Symfony mailer component without the use of the Symfony framework.
Go to file
Adam 9523759e0c docs: fix grammar 2022-12-04 00:10:13 -06:00
.gitignore Initial commit 2022-12-03 23:41:35 -06:00
LICENSE docs: add software license 2022-12-04 00:00:35 -06:00
README.md docs: fix grammar 2022-12-04 00:10:13 -06:00
composer.json build: restrict to php v8.0.0 2022-12-04 00:03:40 -06:00
composer.lock build: restrict to php v8.0.0 2022-12-04 00:03:40 -06:00
mailer.php Initial commit 2022-12-03 23:41:35 -06:00

README.md

Symfony Mailer Example

It is quite often a common practice now to see many websites with a contact form that will send the message to the recipient via email. What is not always clear is how to achieve this. My intent here is to demonstrate how to send email messages using the Symfony mailer component without using the Symfony framework. To keep things simple the PHP code will be run via the command line and use SMTP (simple mail transfer protocol) for the mail transport. I will not be covering how to create the HTML form, how to capture errors/exceptions, but rather focus on the just the act of sending an email message using PHP.

System Requirements

The following must be installed on the system prior to development.

  • Composer v2.4.4
  • PHP v8.0.0

Development Setup

  1. Clone the project locally.

    $ git clone https://git.nixnet.services/adamsdesk/symfony-mailer-example.git
    
  2. Install all dependencies.

    $ composer install
    
  3. Create Configuration File

    This configuration file is used by Symfony mailer to authenticate to the email server in order to send an email message. It is best practice to store this information separately to help avoid people stealing it.

    $ nano .env
    
    MAILER_PWD="enter-password"
    MAILER_PREFIX="smtp://username@example.com:"
    MAILER_SUFFIX="@mail.example.com:465"
    

    Note: Enclose the variables in double quotes to avoid the value being truncated

    Note: TLS (SSL) peer verification is done by default. This can be disabled by adding "?verify_peer=0" to the end of MAILER_SUFFIX (e.g. MAILER_SUFFIX="@mail.example.com:465?verify_peer=0"), though this is not advisable.

Run The Code

At this stage the code is ready. A successful run will return the prompt and no message will be displayed in the terminal. Check your email inbox for the sent email message.

$ php mailer.php

The copyright license (LICENSE) is only referring to the source code.