Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

PHP Email Script

Hello Experts,

I'm new to PHP, can someone tell me a basic script on how to send e-mail with PHP, and what do I need server-wise?

Thank you./
0
APD Toronto
Asked:
APD Toronto
  • 5
  • 4
  • 3
2 Solutions
 
GaryCommented:
$headers = "from@here.com\r\n"; / the From email address
$headers .= "MIME-Version: 1.0\r\n";
// If you want to send an HTML email
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$subject = "Email subject";
$message = "The body message";
$recipient = "you@yoursite.com"; // Who you are sending the email to

mail($recipient, $subject, $message, "From: $headers \n");

Open in new window


And of course you need an email server
0
 
Ray PaseurCommented:
If you're a glutton for punishment, extended hours of debugging and lots of non-value-added activity, I can hold my nose and point to this:
http://php.net/manual/en/function.mail.php

If you're interested in results that can be gotten quickly, easily and reliably, I am pleased to recommend this:
http://www.constantcontact.com/index.jsp

If you want to tell us a little more about the nature of the application perhaps we can offer some concrete suggestions.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
APD TorontoAuthor Commented:
This is basically for a contact form, and sending purchase invoices.

My mail server also requires authentication, so the above isn't working.

In ASP, I m successful by using CDO, where I can have any "TO" address, but authenticate with a internal e-mail address and password.
0
 
Ray PaseurCommented:
Here's a basic form-to-email script.  If this is going on a public-facing server you might want to put in a password check or a CAPTCHA test.

<?php // demo/form_to_email.php
error_reporting(E_ALL);

// SEND MAIL FROM A FORM

// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from = 'NoReply@Your.org';
$subj = 'Contact Form';

// THIS IS AN ARRAY OF RECIPIENTS - CHANGE THESE FOR YOUR WORK
$to[] = 'You@Your.org';
$to[] = 'Her@Your.org';
$to[] = 'Him@Your.org';


// IF THE DATA HAS BEEN POSTED (AT LEAST SOMETHING IN THE EMAIL INPUT CONTROL)
if (!empty($_POST['e']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $safe_mail = clean_string($_POST['e']);
    $safe_name = clean_string($_POST['n']);
    $safe_fone = clean_string($_POST['t']);
    $safe_idea = clean_string($_POST['i']);

    // CONSTRUCT THE MESSAGE THROUGH STRING CONCATENATION
    $content = NULL;
    $content .= "You have a New Query From $safe_name" . PHP_EOL . PHP_EOL;
    $content .= "Tel No: $safe_fone" . PHP_EOL;
    $content .= "Email: $safe_mail" . PHP_EOL;
    $content .= "Idea: $safe_idea" . PHP_EOL;

    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo PHP_EOL . "<br/>MAIL FAILED FOR $recipient";
        }
        else
        {
            echo PHP_EOL . "<br/>MAIL WORKED FOR $recipient";
        }
    }

    // PRODUCE THE THANK-YOU PAGE
    echo '<p>THANK YOU</p>' . PHP_EOL;
    die('TASK COMPLETE');
}


// A FORM TO TAKE CLIENT INPUT FOR THIS SCRIPT
$form = <<<ENDFORM
<form method="post">
Please enter your contact information
<br/>Email: <input name="e" />
<br/>Phone: <input name="t" />
<br/>Name:  <input name="n" />
<br/>Ideas? <textarea name="i"></textarea>
<br/><input type="submit" />
</form>
ENDFORM;

echo $form;


// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    // IF MAGIC QUOTES IS ON, WE NEED TO REMOVE SLASHES
    $str = stripslashes($str);

    // REMOVE EXCESS WHITESPACE
    $rgx
    = '#'                // REGEX DELIMITER
    . '\s'               // MATCH THE WHITESPACE CHARACTER(S)
    . '\s+'              // MORE THAN ONE CONTIGUOUS INSTANCE OF WHITESPACE
    . '#'                // REGEX DELIMITER
    ;
    $str = preg_replace($rgx, ' ', $str);

    // REMOVE UNWANTED CHARACTERS
    $rgx
    = '#'                // REGEX DELIMITER
    . '['                // START OF A CHARACTER CLASS
    . '^'                // NEGATION - MATCH NONE OF THE CHARACTERS IN THIS CLASS
    . 'A-Z0-9'           // KEEP LETTERS AND NUMBERS
    . '"'                // KEEP DOUBLE QUOTES
    . "'"                // KEEP SINGLE QUOTES
    . '@&+:?_.,/\-'      // KEEP SOME SPECIAL CHARACTERS (ESCAPED HYPHEN)
    . ' '                // KEEP BLANKS
    . ']'                // END OF THE CHARACTER CLASS
    . '#'                // REGEX DELIMITER
    . 'i'                // CASE-INSENSITIVE
    ;
    $str = preg_replace($rgx, NULL, $str);

    // REMOVE EXCESSIVE INPUT
    $str = substr($str, 0, 255);

    return trim($str);
}

Open in new window

0
 
GaryCommented:
The built in mailer doesn't support authentication as it assumes a local server that doesn't require it for local mail.
Your best alternative is
http://sourceforge.net/projects/phpmailer/
0
 
APD TorontoAuthor Commented:
Same issue... I need to authenticate.
0
 
GaryCommented:
Uh, did you read thru the documentation
https://github.com/PHPMailer/PHPMailer
0
 
APD TorontoAuthor Commented:
My web host is recommending PEAR.

So, I found http://pear.php.net/manual/en/installation.getting.php and downloaded go-pear.phar into C:\php and tried to run go-pear.phar, but I get the "Windows Can't open this file... use the web or select a program" window, so I have the following 2 questions:

1. As I read that phar is an archive, should I be storing it directly into my C:\php, or should I make a sub directory?

2. How do I run the phar file?
0
 
Ray PaseurCommented:
I would have to defer to your web host for help with their recommendation.  My recommendation, based on years of experience in this area, is Constant Contact.  I'll sign off on this now.  Best of luck with the project, ~Ray
0
 
APD TorontoAuthor Commented:
By my understanding, Constant Contact is for e-mail broadcasting, but if you refer above, this is for a contact form and sending receipts after purchase - in other words, personalized.

With this in mind, can any one please give me some input regarding the 2 questions from my previous post on PEAR?
0
 
GaryCommented:
It's a batch file that you run from the command line - shows you how in the link.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now