Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Email Script

Posted on 2014-09-26
12
Medium Priority
?
165 Views
Last Modified: 2014-10-01
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
Comment
Question by:APD_Toronto
  • 5
  • 4
  • 3
12 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 1000 total points
ID: 40346240
$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
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 40346277
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:APD_Toronto
ID: 40346301
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40346311
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
 
LVL 58

Expert Comment

by:Gary
ID: 40346387
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
 

Author Comment

by:APD_Toronto
ID: 40346389
Same issue... I need to authenticate.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40346400
Uh, did you read thru the documentation
https://github.com/PHPMailer/PHPMailer
0
 

Author Comment

by:APD_Toronto
ID: 40351181
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40351221
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
 

Author Comment

by:APD_Toronto
ID: 40352393
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
 
LVL 58

Expert Comment

by:Gary
ID: 40355245
It's a batch file that you run from the command line - shows you how in the link.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question