Solved

PHP Email Script

Posted on 2014-09-26
12
146 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 250 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 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 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
 
LVL 58

Expert Comment

by:Gary
ID: 40346281
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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 109

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 109

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress Pagination Function Not working ? 7 39
PHP error function not working on AWS 10 74
mysql query for sum() 3 28
reverse engineer .sql from php files 11 35
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

830 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