The ability to send emails straight from your website has its benefits. In as much as we have
some of the IT companies offering this service free of charge; it will be wise for web developers
to understand embedding email functionality on their site or that of their clients.
One of the benefits of this feature is sign up verification. If you've noticed, before you complete registration on most website, a link is generated dynamically and sent to your email account for verification. This refrains fraudsters or illegal use of someone else account. Making you the sole owner of that email account since you possess both the username and password.
In addition to account verification; you can use this feature to send newsletter to your subscribers. Keeping in touch with them.
The list can go on and on depending on what you intend to use this PHP email feature to do. Let's quickly dive into how to implement this on your website or blog.
First things first. You cannot be able to send mails using this PHP email feature on your local server. Your files for this exercise must be uploaded to a live server (i.e on the internet) before this feature can be used.
The PHP mail function is the core brain behind this operation. It returns a true or false value depending on the outcome of the task at hand. This function basically takes three parameters or arguments in addition to an optional one
making four arguments in all. The first argument is of string data-type and refers to the recipient of the mail. This is commonly referred to as "TO". Whosoever you intend to send the mail to. Next to that is the subject of the mail; also of string data-type. Following is the body of the mail. That is the content of the mail. Lastly is an optional
parameter which could be the Bcc or Cc feature. These parameters must be followed sequentially without interchanging their places to make sure the operation runs successfully.
Lets look at a little demo below to fully understand what this is all about.
//Variable defintion (Email information)
$to = "audience@talkingmail.com";
$subject = "Sending email...";
$body = "I will like to use this PHP email feature.";
$headers = "From: talktojoegee@gmail.com";
//send mail
if(mail($to,$subject,$body,$headers)){
echo"Mail sent successfully";
}else{
echo"Something went wrong.";
}
NOTE: Though headers is optional while sending email. If its not explicitly declared in your PHP script that would handle
the email processing; it must be clearly defined in your php.ini file. It specifies parameters like From, Cc, Bcc. The
additional parameters should be separated
with a CRLF (\r\n)
Since mail function returns a boolean value (i.e true or false), we are using an if conditional statement to determine whether
the mail was delivered or not.
One more thing before we wrap up on this. If you notice, the mail you receive most times displays maybe the company name instead
of the email address of the sender. For example, talktojoegee@gmail.com sent an email to you. Instead of seeing the email
address, the company name; lets say Light Technologies which is the company name for the talktojoegee@gmail displayed. To do that,
we'll have to modify the header a little. Modify the header to -
$headers = "From: Light Technologies <talktojoegee@gmail.com>";With this, we can say that we've come to the end to this sending email with PHP lesson. Explore more on this in areas like sending email to multiple people at the same time, sending an email with an attachment among others and feel free to share your findings here with us. Thank you.

0 comments:
Post a Comment