PHP
mail is the built-in PHP function that is used to send emails from PHP scripts.
The
mail function accepts the following parameters;
- Email
address
- Subject
- Message
- CC
or BC email addresses
o A cost-effective way of notifying users on important events.
o Users
can contact you via email by providing a contact us form on the website by
emails.
o Developers
can use it to receive system errors by email
o Use
it to email for newsletter subscribers.
o Use
it to send password reset links to users who forget their passwords
o Use
it to email activation/confirmation links for registering users and verifying
their email addresses.
Configuration php.ini for mailing
Windows
users should ensure two directives. The first is SMTP that defines your email
server address. The second is sendmail_from which defines your own email
address.
The configuration for Windows −
[mail
function]
;
For Win32 only.
SMTP
= smtp.secureserver.net
; For win32 only
sendmail_from
= pankajdadhich1@gmail.com.com
Linux
users simply set php location of their sendmail application.
The path and others should be specified to the sendmail_path directive.
The
configuration for Linux −
[mail
function]
;
For Win32 only.
SMTP
=
; For win32 only
sendmail_from
=
; For Unix only
sendmail_path
= /usr/sbin/sendmail -t -i
The PHP mail() Function
In the PHP use built-in mail() function for creating and sending
email messages to one or more recipients dynamically from our PHP application
by a plain-text form or formatted HTML.
The basic syntax of this function can be given
with:
mail(to, subject, message, headers, parameters)
Parameter |
Description |
Required —
The following parameters are required |
|
to |
The
recipient's email address. |
subject |
Subject
of the email to be sent. This parameter i.e. the subject line cannot contain
any newline character (\n). |
message |
Defines
the message to be sent. Each line should be separated with a line feed-LF (\n). Lines should not exceed 70 characters. |
Optional —
The following parameters are optional |
|
headers |
This
is typically used to add extra headers such as "From",
"Cc", "Bcc". The additional headers should be separated
with a carriage return plus a line feed-CRLF (\r\n). |
parameters |
Used
to pass additional parameters. |
Sending Plain Text
Emails
The simplest way to send an email with PHP.
Example:- first declare the variables — recipient's
email address, subject line and message body — then we pass these variables to
the mail() function to send
the email.
Example
<?php
$to = abcd@email.com';
$subject = 'course
information';
$message = 'pls
tell the fee of bac-i?';
$from = 'xyz@email.com';
// Sending email
if(mail($to,
$subject, $message)){
echo 'Your mail has been sent
successfully.';
} else{
echo 'Unable to send email. Please try
again.';
}
?>
Sending HTML
Formatted Emails
To improve that output, and make the email
into an HTML-formatted email.
To send an HTML email, the process will be the
same. But, this time we need to provide additional headers as well as an HTML
formatted message.
Example
<?php
$to =
abcd@email.com';
$subject = 'course
information';
$from = 'xyz@email.com';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .=
'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From:
'.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message =
'<html><body>';
$message .= '<h1
style="color:#f40;">Hi Jane!</h1>';
$message .= '<p
style="color:#080;font-size:18px;"> pls tell the fee of bac-III?</p>';
$message .=
'</body></html>';
// Sending email
if(mail($to,
$subject, $message, $headers)){
echo 'Your mail has been sent
successfully.';
} else{
echo 'Unable to send email. Please try
again.';
}
?>
Note: the PHP mail() function
is a part of the PHP core but you need to set up a mail server on your machine
to make it really work.
=============================================================
PHP
HEADERS:-
An optional part of the email.
it includes information such as CC, BCC.
CC (carbon copy)
It’s used when you want to send a copy to an interested person i.e. a
complaint email sent to a company can also be sent as CC to the complaints
board.
BCC (blind carbon copy)
It is similar to CC. The email addresses included in the BCC section
will not be shown to the other recipients.
Example :-
// To send HTML
mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .=
'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From:
'.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
SIMPLE MAIL TRANSMISSION PROTOCOL (SMTP)
PHP mailer uses SMTP to send mail.
On the hosted server, the SMTP settings would
have already been set.
The SMTP mail settings can be configured from
“php.ini” file in the PHP installation folder.
Configuring SMTP settings on localhost.
- Click on Find and search mail functions
- Locate
the entries
- [mail
function]
- ;
XAMPP: Don’t
remove the semi column if you want to work with an SMTP Server like
Mercury
- ;
SMTP = localhost
- ;
smtp_port = 25
- Remove the semi
colons before SMTP and smtp_port and set the SMTP to your SMTP server and
the port to your SMTP port.
- SMTP =
smtp.example.com
- smtp_port = 25
- Note the
SMTP settings can be gotten from your web hosting providers.
- If
the server requires authentication, then add the following lines.
- auth_username
= example_username@example.com
- auth_password
= example_password
- Save
the new changes.
- Restart Apache server.
Reviewing SMTP
- First,
break the SMTP client and SMTP server into two components user agent (UA)
and mail transfer agent (MTA).
- The
user-agent (UA) prepares the message, creates the wrapper, and then puts the message in the packet. The mail transfer agent (MTA) transfers this mail across the internet.
- SMTP
allows a more complex system by adding a relaying system.
- one
MTA at sending side and one at receiving side, more MTAs can be added,
acting either as a client or server to relay the email.
- SMTP
allows a more complex system by adding a relaying system.
Working of SMTP
- Composition
of Mail:
A user
sends an e-mail by composing an electronic mail message using a Mail User Agent
(MUA). Mail User Agent is a program that is used to send and receive mail. The message contains two parts: body and header. The body is the main part of the
message while the header includes information such as the sender and recipient
address. The header also includes descriptive information such as the subject
of the message. In this case, the message body is like a letter and the header is
like an envelope that contains the recipient's address.
- Submission
of Mail:
After
composing an email, the mail client then submits the completed e-mail to the
SMTP server by using SMTP on TCP port 25 (for Gmail use 587).
- Delivery
of Mail:
E-mail
addresses contain two parts: username of the recipient and domain name. For
example, sjkpgm@gmail.com, where "sjkpgm" is the username of the
recipient and "gmail.com" is the domain name.
If the domain name of the recipient's email address is different from the
sender's domain name, then MSA will send the mail to the Mail Transfer Agent
(MTA). To relay the email, the MTA will find the target domain. It checks the
MX record from Domain Name System to obtain the target domain. The MX record
contains the domain name and IP address of the recipient's domain. Once the record
is located, MTA connects to the exchange server to relay the message.
- Receipt
and Processing of Mail:
Once
the incoming message is received, the exchange server delivers it to the
incoming server (Mail Delivery Agent) which stores the e-mail where it waits
for the user to retrieve it.
- Access
and Retrieval of Mail:
The
stored email in MDA can be retrieved by using MUA (Mail User Agent). MUA can be
accessed by using login and password.
=======================================================================
PHPMailer
PHPMailer is the classic email sending library for PHP.
It uses several ways of sending email
messages such as mail(), Sendmail, qmail, and direct dispatch to SMTP servers.
it provides a list of advanced
features:
- SMTP authentication
- Secure/MIME (Multipurpose Internet Mail Extensions) encryption
- Support of Transport Layer Security (TLS) and Secure Sockets Layer (SSL) protocols
- HTML content along with plain text
- Multiple fs(file specification), string, and binary attachments
- Embedded images support.
Sending Email With PHPMailer and SMTP
To send emails with PHPMailer and
SMTP, first to install PHPMailer and configure SMTP settings.
0 Comments