Announcement

Collapse
No announcement yet.

Help requested on SMTP PHP mail form

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help requested on SMTP PHP mail form

    I have installed the required forms for a contact form (which work perfectly), BUT I would like it to send to 2 people (me and one other); the code reads like this at present and I would like to know how to add another recipient (either as a 2nd recipient direct OR as a CC).

    The code comes from the standard contact.php script which I was sent by eUKhost.

    Can anyone help?


    -------------------------------------

    //Your SMTP servers details

    $mail->IsSMTP(); // set mailer to use SMTP
    $mail->Host = "localhost"; // specify main and backup server or localhost
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->Username = "me[at]address[dot]com"; // SMTP username
    $mail->Password = "********"; // SMTP password
    //It should be same as that of the SMTP user

    -------------------------------------

    Thank you in advance for any help!

  • #2
    Re: Help requested on SMTP PHP mail form

    Hi Woggy,

    Apologies for the delayed reply - I have only just seen your post. I'd recommend you use the PEAR Mail library to accomplish this. Before doing so, make sure you have an email address set up in cPanel or Plesk (whichever control panel you use) for sending emails using the script below. Change the $username, $password, $subject, $from, $to and $cc values below as needed. Please note I have not tested the script below after writing it, so please make sure it works correctly in a separate PHP file before using it anywhere.

    PHP Code:
    <?php

    include('Mail.php');

    $host 'localhost'// only change this if your mail is sent by a server other than the one your website is hosted on

    $username 'enter your email address';
    $password 'enter the password for your email address';
    $subject 'the subject line of your e-mail';
    $from 'enter your email address'// same as $username above (your email address)
    $to 'person to send to';
    $cc 'person to CC';
    $body 'Email message here';

    $recipients $to ", " $cc// joins both $to and $cc together

    $headers = array ('From' => $from,
    'To' => $to,
    'Cc' => $cc,
    'Subject' => $subject); // array for the "email header"

    $smtp Mail::factory('smtp',
    array (
    'host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password)); // puts things in place to send the email - mail server address, whether authentication is required (yes), email username and password

    $mail $smtp->send($recipients$headers$body); // sends the email

    ?>
    If you receive a PHP inclusion error when attempting to use the script above, it is probably because the PEAR Mail package is not installed on the server your website is hosted on, or is not located in the extensions directory specified in the server php.ini PHP Configurable File. In any case, let me know and I will get the package installed on the server your account is located on.

    Notes:

    Warning: If you accept any user input (e.g. through a contact form), make sure you sanitise user input by wrapping the submitted data/text in the htmlspecialchars() PHP function, which prevents HTML code being parsed by e-mail clients. You should also validate whether an e-mail address is correctly formed using the filter_var() PHP function like this: filter_var('enter the email address here', FILTER_VALIDATE_EMAIL) - see this page for full details).

    Dedicated/VPS users: You need the Mail and Net_SMTP PEAR packages to send e-mail via the PEAR library (you'll also need Mail_Mime for sending HTML e-mails). You can install these PEAR packages via SSH using:

    Code:
    pear install mail
    
    pear install Net_SMTP
    
    pear install Mail_Mime
    Including HTML in your e-mails or attempting to send an HTML-formatted e-mail in the code example above will not work. To send HTML e-mails, see the code example on the pear.php.net website: https://pear.php.net/manual/en/packa...me.example.php. For plaintext emails with line breaks, make sure there are no tabs that are automatically created by your programming/text editor as these will otherwise be included in the email content.

    Kind Regards,

    Ben Stones,

    Comment


    • #3
      Re: Help requested on SMTP PHP mail form

      Hi Ben,

      Thank you for the comprehensive response - will give it a go!

      Comment

      Working...
      X