Sending Email from PHP

Sending E-mail from PHP

For Sending external emails from websites to to domains that do not exist on our mail servers, the regular PHP mail() functions can not be used  as  SMTP authentication is required  for mail relay.   If using Joomla or wordpress, you can change them to use SMTP authentication within the program.

If you are using a generic script that relies on the mail() function to send external emails then you will need to enable smtp authentication to send mails via our servers.      Below is a fully functional script  to send e-mails submittedd from a contact form. The username,  password and server name will need to be replaced with a valid email account   on your domain that is running on our servers to allow sending mails.

NOTE   !!!   Please remove  the asterix's  *   in the red php tag lines

<*?*php
if ($_POST["email"]<>'') {

//NOTE if using php 5.4 or earlier use
require_once('class.phpmailer.php');

//NOTE if using php 5.5 or older uncomment this line and comment out the class.phpmailer.php line above
//require_once('phpmailerautoload.php');  

//Create a new PHPMailer instance
$mail = new PHPMailer;

 

//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

 

//Set the hostname of the mail server
$mail->Host = "smtp.yourdomain.co.za";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "validuser@yourdomain.co.za";
//Password to use for SMTP authentication
$mail->Password = "validuserspassword";

 


//Set who the message is to be sent from
$mail->setFrom('fromwho@yourdomain.co.za','First Last');

 

//Set an alternative reply-to address
//$mail->addReplyTo('$_POST["email', 'First Last');

 

//Set who the message is to be sent to
$mail->addAddress('towho@yourdomain.co.za', 'website contact');

 

//Set the subject line
$mail->Subject = 'Website Contact Feedback';

 

$message=
'Name:   '.$_POST['name'].'<br />
Email:   '.$_POST['email'].'<br />
Company:   '.$_POST['company'].'<br />
Telephone:   '.$_POST['telephone'].'<br />
Comments:     '.$_POST['comment'].'<br />
'.nl2br($_POST['message']).'
';

 

$mail->msgHTML($message);
//Replace the plain text body with one created manually
//$mail->AltBody = 'This is a plain-text message body';

 

//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors

 

//if (!$mail->send()) {
//echo "Mailer Error: " . $mail->ErrorInfo;
//} else {
//echo "Your Message was successfully sent!";
//}

 

if(!$mail->send()) {
              echo '<p class="contact-message">Message could not be sent.</p>';
              echo '<p class="contact-message">Mailer Error: ' . $mail->ErrorInfo . '</p>';
      } else {
              echo '<br><br><p class="contact-message" align="center">Your message has been sent. We will be in touch.';
      }  
?*>

 

<*?*php
} else {
?*>
<form action="mailsend.php" method="post">
<table width="500" border="0" align="left" cellpadding="0" cellspacing="2">
<tr>
<td width="29%" class="bodytext">Name:</td>
<td width="71%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td class="bodytext">Email Address:</td>
<td><input name="email" type="text" id="email" size="32"></td>
</tr>
<tr>
      <td class="bodytext">Company</td>
      <td><input name="company" type="text" id="company" size="32" /></td>
</tr>
<tr>
      <td class="bodytext">Telephone</td>
      <td><input name="telephone" type="text" id="telephone" size="32" /></td>
</tr>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"></textarea></td>
</tr>
<tr>
<td class="bodytext"> </td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
<*?*php
};
?*>

 

  • 28 Users Found This Useful
Was this answer helpful?

Related Articles

Sending email from ASP.Net

Sending E-mails from ASP.NetStep 1: Place the following in your web.config file, making sure to...

Sending mail with Joomla websites

1. Enter your Joomla dashboard. 2. Click on "Site", "Global configuration". 3. Choose the...

Sending smtp mail with wordpress

We do not allow any mails to be relayed without first authenticating to our mail servers,...

Sending email from asp pages

This portion goes into the header portion of the page before the  tag.   (Just remove the space...

Sending emails from a website using ASP

Please use the following ASP code to send e-mails, note that our mail servers require SMTP...