Login

Username or Password is Incorrect!
Enter your Username and Password!

Username

Password

Forgot passsword?

Topics: 15   |   Posts: 13   |   Comments: 12   |   Members: 165

  • 2
  • 0

Sending Emails with PHP

omari Sun Mar 25, 2012 - 04:44 am

Omari Celestine

If you are looking to send email to your users or friends directly from your server, then this might be the topic for you. I’m just going to display the basic fundamentals of a PHP email along with the necessities to ensure functionality, delivery and user credibility.

So basically, with php emails, we will be using a pre-built function called mail() and you can use any text editor or text based web builder like Notepad++ to create your mailer. Within this function, there are four basic components that we will need. These include;


  1. Receiver (who is receiving the email ([email protected])).

  2. Subject – instant declaration of your message so users know what it is about before opening it.

  3. Body – the message which we will be sending to our user.

  4. Headers – these will define our email with the necessary components to validate our message as an actual mail.

  5. Senders address – which will let the users and their clients know who is sending the message.



So let's begin by defining our first few variables:

<?php

// who is receiving the message
$to = "[email protected]";

// the name from which the message is being sent
$from_name = "[myWebsite]";

// the email address from which the message is being sent
$from_address = "[email protected]";

// main point of the message or why it is being sent
$subject = "New comments of your post: 'Building a proposal' on [myWebsite]";
// the actual message to be sent
$body = "

Hello Jain,

We would like to notify you that you recently receiver 15 comments on your profile post titled 'Building a proposal'.

You can login to your account from http://mywebsite.com/login to view all updates and feedback you received from your friends on [myWebsite].

Sincerely,

John Doe
Notifications Manager
[myWebsite]";

// required components of the email
$headers = "MIME-Version: 1.0\r\n"
  ."Content-Type: text/plain; charset=utf-8\r\n"
  ."Content-Transfer-Encoding: 8bit\r\n"
  ."From: =?UTF-8?B?". base64_encode($from_name) ."?= <$from_address>\r\n"
  ."X-Mailer: PHP/". phpversion();

// call our mail() function and send the message
mail($to, $subject, $body, $headers, "-f $from_address");
?>



Now with this, there is one requirement of you relative to your server. The email from which you are sending message needs to be created and valid for security reasons.

This is just the simplest method (that I know of) for sending a message to another person. For additional information or more detailed instructions, you can contact me at [email protected].

0 ATTACHMENTS

Login or Register to leave a comment.

Topic Search
Get Started