Home / PHP/MySQL Tutorials / Archive by category 'PHP Mail'

PHP Mail

Store email and username in mysql database using php

<?php
 //opens connnection to mysql server
 
$dbc = mysql_connect('localhost','root','');
if (!dbc){
    die('Not Connected:' . mysql_error());
    }
//select a database
 
$db_selected = mysql_select_db("rivals",$dbc);
if(!$db_selected)
{
    die("can't connect :" . mysql_error());
}
 
$query = "UPDATE `database` SET email='something' WHERE username='rivals'";
$result = mysql_query($query, $dbc) or die(mysql_error());
 
$username = $_GET['username'];
$email = $_GET['email'];
$password = $_GET['password'];
$insert = "INSERT INTO `database` (`username`, `email`, `password`) VALUES ('$username', '$email', '$password')";
 
if(isset($username) && isset($email) && isset($password)){
$addnew = mysql_query($insert, $dbc) or die(mysql_error());
}
 
if($addnew){
echo "Username [".$username."] with Email Address [".$email."] and password[".$password."]<br><br>";
}
 
// just a message
echo "it's done"
?>

imap_last_error() function in PHP

This function returns the full text of the last IMAP error message that occurred on the current page. Returns FALSE if no error messages are available. It is useful in connecting to an IMAP server as shown.

$mailbox = "{mail.myhost.com:143}INBOX";
$user = "me@myhost.com"; 
$pass = "mypassword"; 
 
$connection = imap_open($mailbox,$user,$pass) or die(imap_last_error()."<br>Connection Faliure!");

Get list of mailboxes using IMAP in PHP

<?php
$imbox = imap_open("{imap.site.com:143}", "username", "password");
 
echo "<h1>Mailboxes</h1>\n";
$folders = imap_listmailbox($imbox, "{imap.site.com:143}", "*");
 
if ($folders == false) {
    echo "failed to retrieve folders<br />\n";
} else {
    foreach ($folders as $val) {
        echo $val . "<br />\n";
    }
}

Open a stream to an IMAP mailbox in PHP

Developers can use the imap_open() php function, which enables you to open a stream to a IMAP server.

Example to open a connection to an IMAP server:
// To connect to an IMAP server running on port 143 on the server.com machine

<?php
$imaphandle = imap_open("{servername.com:143}INBOX", $user, $pass);
?>

The first argument is the mailbox name. The server part, which is enclosed in ‘{‘ and ‘}’, consists of the servers name or ip address, an optional port (prefixed by ‘:’), and an optional protocol specification (prefixed by ‘/’). The second and third arguments to imap_open() are your username and passwords, respectively.


Send email with CC(Carbon Copy) & BCC(Blind Carbon Copy)

The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn’t use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).
Second, the custom headers like From:, Cc:, Bcc: and Date: are not interpreted by the MTA in the first place, but are parsed by PHP.

As such, the to parameter should not be an address in the form of “Something “. The mail command may not parse this properly while talking with the MTA.

The php mail function accepts the following parameters.

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise. It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

The server just flushes out the mail through its smtp port/pop3 port.

<html>
  <head>
  <title>Send email with CC and BCC</title>
  </head>
  <body>
  <form action="sendemail.php" method=post name=form1>
  <table>
    <tbody>
    <tr>
      <td>
       <div align=right><b>To</b></div></td>
      <td>
        <p>Name <input name=mailtoname size=35><br />E-mail
                <input name=mailtomail size=35></p></td></tr>
    <tr>
      <td>
        <div align=right><b>CC</b></div></td>
      <td><input name=mailcc size=35> </td></tr>
    <tr>
      <td>
        <div align=right><b>BCC</b></div></td>
      <td><input name=mailbcc size=35> </td></tr>
    <tr>
      <td>
        <div align=right><b>Priority</b></div></td>
      <td><select name=mailpriority>
            <option value=1>Highest</option>
            <option value=2>High</option>
            <option selected value=3>Normal</option>
            <option value=4>Low</option>
            <option value=5>Lowest</option>
          </select>
      </td></tr>
    <tr>
      <td><div align=right><b>Subject</b></div></td>
      <td><input name=mailsubject size=35></td></tr>
    <tr>
      <td>
        <div align=right><b>Message</b> </div></td>
      <td><textarea cols=50 name=mailbody rows=7></textarea> </td></tr>
    <tr>
      <td colSpan=2>
        <div align=center><input name=Submit type=submit value=Submit></div>
    </td>
    </tr>
   </tbody>
   </table>
  </form>
  </body>
  </html>

save as : sendemail.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  <html>
  <head>
  <title>Send Mail Script</title>
  </head>
  <body>
  <?php
 
    $message= " " ;
    if (empty ( $mailtoname) || empty ( $mailtomail) ) {
       die ( "Recipient is blank! ") ;
    }else{
       $to = $mailtoname . " <" . $mailtomail . ">" ;
    }
 
    if ( empty ( $mailsubject) ) {
      $mailsubject=" ";
    }
 
    if (($mailpriority>0) && ($mailpriority<6)) {
       $mailheader = "X-Priority: ". $mailpriority ."\n";
    }
 
    $mailheader.= "From: " . "Sales Team <sales@yourdomain.com>\n";
    $mailheader.= "X-Sender: " . "support@yourdomain.com\n";
    $mailheader.= "Return-Path: " . "support@yourdomain.com\n";
 
    if (!empty($mailcc)) {
      $mailheader.= "Cc: " . $mailcc ."\n";
    }
 
    if (!empty($mailbcc)) {
      $mailheader.= "Bcc: " . $mailbcc ."\n";
    }
 
    if (empty($mailbody)) {
      $mailbody=" ";
    }
 
    $result = mail ($to, $mailsubject, $mailbody, $mailheader);
    echo "<center><b>Mail sent to ". "$to". "<br />";
    echo $mailsubject. "<br />";
    echo $mailbody. "<br />";
    echo $mailheader. "<br />";
    if ($result) {
       echo "<p><b>Email sent successfully!</b></p>";
    }else{
       echo "<p><b>Email could not be sent. </b></p>";
    }
  ?>
  <div align="center">
  <table><tr><td width="66"><div align="right"><b>To</b></div></td>
             <td width="308"><b><?php echo $mailtoname . " [". $mailtomail . " ]";?></b></td></tr>
 
         <tr><td width="66"><div align="right"><b>CC</b></div></td>
             <td width="308"><b><?php echo $mailcc;?></b></td></tr>
         <tr><td width="66"><div align="right"><b>BCC</b></div></td>
             <td width="308"><b><?php echo $mailbcc; ?></b></td></tr>
         <tr><td width="66"><div align="right"><b>Priority</b></div></td>
             <td width="308"><b><?php echo $mailpriority;?></b></td></tr>
         <tr><td width="66"><div align="right"><b>Subject </b></div></td>
             <td width="308"><b><?php echo $mailsubject;?></b></td></tr>
         <tr><td width="66"><div align="right"><b>Message</b></div></td>
             <td width="308"><b><?php echo $mailbody;?></b></td></tr>
  </table>
  </div>
  </body>
  </html>

Simple email with php

Create a HTML Form that Accepts Mail-Related Information. The form must post to a php file and mail() function should be enabled on the server to complete sending email.

Save as sendmail.html

<html>
  <head>
  <title>Simple Send Mail Form w3mentor.com</title>
  </head>
  <body>
  <h1>Mail Form</h1>
  <form name="form1" method="post" action="mail.php">
  <table>
      <tr><td><b>To</b></td><td><input type="text" name="mailto" size="35"></td></tr>
      <tr><td><b>Subject</b></td>
          <td><input type="text" name="mailsubject" size="35"></td></tr>
      <tr><td><b>Message</b></td>
          <td><textarea name="mailbody" cols="50" rows="7"></textarea></td>
      </tr>
      <tr><td colspan="2">
            <input type="submit" name="Submit" value="Send">
          </td>
      </tr>
   </table>
 </form>
 </body>
 </html>

Save as: mail.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
  <?php
    if (empty ($_POST['mailto']) ) {
       die ( "Recipient is blank! ") ;
    }
 
    if (empty ($_POST['$mailsubject']) ){
       $mailsubject=" " ;
    }
 
    if (empty ($_POST['$mailbody']) ) {
       $mailbody=" " ;
    }
 
    $result = mail ($mailto, $mailsubject, $mailbody) ; //send the email
 
    if ($result) {
       echo "Email sent successfully!" ;
    }else{
       echo "Email could not be sent." ;
    }
?>