Tutorials > General Webhosting Info > Sending email from my Website using PHP and PHPMailer Class
Sending email from my Website using PHP and PHPMailer ClassSending email from your website or online application can be achieved in a number of ways. PHP’s mail function can be used to send a quick and simple text email. The PHPMailer class can also be used to send far more complex emails, HTML emails and more. Below is a quick and easy method that can be use on any RSAWeb Linux Webhosting Server.
Mail Function in PHP
I would like to send, “Received your test message from RSAWeb’s Webhosting Servers” to *protected email* with the subject “Test Message from RSAWeb”. Feel free to modify this code to suite your requirements.
The code will look like:
<?php
$fromname = ‘RSAWeb Website’; // fromname
$from = 'fromtest@fromtest.com'; // from email address
$to = ‘*protected email*’; // enter your email or relevant email address here
$subject = ‘Test Message from RSAWeb’;
$message = ‘Received your test message from RSAWeb’s Webhosting Servers’;
$headers = "From: $fromname <$from> \r\n";
mail ($to, $subject, $message,$headers);
?>
If I wanted to use this in a contact form, for people trying to contact us from our website about our web hosting services, I would post a form with the information they capture on the form. We will use only one php page, it makes coding a bit more complicated but you can do more with it because you can insert error checking into the forms.
Contact.php – copy and paste the code in green below and call the file 'contact.php' - or whatever you choose.
<?php
/* Example copyright by RSAWeb 2005, it may be utilised by RSAWeb Webhosting clients on their hosting accounts. */
if ($action == ‘submit’) // has the form been submitted
{
$from = ‘RSAWeb Website’; // fromname
$to = ‘*protected email*’; // enter your email or relevant email address here
$subject = ‘Test Message from RSAWeb’;
$message = ‘Received a message from ‘ . $_POST[name] . ‘ about RSAWeb’s Webhosting Servers. You can contact them on ‘ . $_POST[cell] . ‘ or ‘ . $_POST[email];
mail ($from, $to, $subject, $message);
?>
<br><br>
<center>Thank you for your submission, we will be contacting you shortly</center>
<?
}
if (!$action) // if there is no value for the action variable, display the contact form
{
?> <form action=’<? echo PHP_SELF; ?>’ method=’POST’>
<? // Make a table with 2 columns, center it and apply some formatting ?>
<table align=’center’ width=’60%’>
<tr>
<td align=’right’>Name: </td>
<td><input type=’text’ name=’name’ value=’’></td>
</tr>
<tr>
<td align=’right’>Cell Number: </td>
<td><input type=’text’ name=’cell’ value=’’></td>
</tr>
<tr>
<td align=’right’>Email Address: </td>
<td><input type=’text’ name=’email’ value=’’></td>
</tr>
<tr>
<td colspan=’2’><input type=’submit’ value=’Submit’></td>
</tr>
</table>
<input type=’hidden’ name=’action’ value=’submit’>
</form>
<?
}
?>
Alternatively you could use the PHPMailer Class. The class is available for download here: http://phpmailer.sourceforge.net and the example code to access the class is below:
<?php
if ($action == "submit"){
include("class.phpmailer.php");
// THIS CALLS THE EMAIL CLASS TO SEND THE EMAIL
$mail = new phpmailer();
$mail->From = "$email";
$mail->FromName = "$name";
// HTML body
$body = "Hello RSAWeb Sales,<BR><BR>";
$body .= "$name is requesting sales contact via the RSAWeb Website<BR><BR>";
$body .= "<b>$name's Details</b><BR><BR>";
$body .= "<b>Mobile: </b> $cell<BR>";
$body .= "<b>Email Address: </b> $email<BR>";
$body .= "<b>Referrer: </b> " . $_SERVER[HTTP_REFERER] . "<BR>";
$body .= "<b>IP Address: </b> " . $_SERVER[REMOTE_ADDR] . "<BR>";
$body .= "<b>Details: </b> $questions<BR>";
// Plain text body (for mail clients that cannot read HTML)
$text_body = "Hi, $name has logged a sales request from your website. \n\n";
$text_body .= "Email: $email \n\n";
$text_body .= "Number: $cell \n\n";
$mail->Subject = "RSAWeb Web Hosting Sales/Info Request";
$mail->Body = $body; // HTML mail content
$mail->AltBody = $text_body; // Text-only mail content
$mail->AddAddress('test@test.com', 'Your name'); // Address to send email to (can repeat this line multiple times for multiple receipients)
// actually send it
$mail->Send();
}
?>
For more examples, please view the phpMailer website for a comprehensive list of options and also to download the class.
Similar Information from this category |