The other day I needed a way to send multiple e-mails with the same message from a specified address. Now there are lots and lots of mailer programs, but after looking around a bit I realized that it would be quicker for me to write a bit of code to do this for me than it would be to find something that fit my needs and get it configured, etc.
Pieces
- Supporting functions
- POST response
- GET response
Each of these sections are commented below. The POST code does the actual mailing, and prints out a list of errors as they are encountered (including e-mails that failed to send). The GET code displays a form for the input parameters. The supporting functions section contains the handleError() function for displaying errors.
Usage
In order to use this code, you’ll need a webserver with mail functionality configured. Your php.ini file should contain the path to sendmail:
[mail function] sendmail_path = /usr/sbin/sendmail
Of course, you’ll also need sendmail configured. If you use the excellent BlueHost or a similar service you’re set up out of the box. Upload the code to your site, navigate to it with a browser, and give it a shot! You should see a form like this (example only, this form does nothing):
Of Note…
The password is hardwired in the code (line 42) – make sure you change this before using it or you’ll open yourself up for abuse!
And that’s about it! Everything should come across straightforward; hopefully this will help you get started with using e-mail in PHP
Open Unformatted Code In New Window
<?php
//*********************************************************
// Supporting Functions
//*********************************************************
/**
* Output the errors and return false or if no error just
* return true.
* @param $errs array of error strings
* @return boolean
*/
function handleErrors($errs)
{
if(isset($errs) != true || count($errs) < 1)
// no errors found
{
return true;
}
// Display errors
//***********************************
echo '<html><body>';
echo '<h1>The following ' . count($errs) . ' error(s) were found:</h1><ol>';
foreach($errs as $err)
{
echo '<li>' . $err . '</li>';
}
echo '</ol></body></html>';
return false;
}
//*********************************************************
// On POST
//*********************************************************
if(isset($_POST['pass']) == true)
{
// Authorization
//****************
if(strcmp($_POST['pass'], 'yourpasswordgoeshere') != 0)
{
$errors[] = 'Access Denied';
}
if(handleErrors($errors) != true)
{
return;
}
// Params
//***************
$subject = $_POST['subject'];
if(isset($subject) != true || strlen($subject) < 1)
{
$errors[] = 'Subject field not set';
}
$from = $_POST['from'];
if(isset($from) != true || strlen($subject) < 1)
{
$errors[] = 'From field not set';
}
$msg = $_POST['msg'];
if(isset($msg) != true || strlen($subject) < 1)
{
$errors[] = 'Message field not set';
}
if(handleErrors($errors) != true)
{
return;
}
// Send the mails
//********************
$headers = 'From: ' . $from . "\r\nReply-To: " . $from;
echo $headers;
$addrs = explode(',', $_POST['addresses']);
foreach($addrs as $addr)
{
$addr = trim($addr);
if (mail($addr, $subject, $msg, $headers) != true)
{
$errors[] = 'Failed to send mail to: ' . $addr;
}
}
if(handleErrors($errors) == true)
{
echo '<html><body><h1>Success!</h1></body></html>';
}
return;
}
//*********************************************************
// On GET
//*********************************************************
?>
<html>
<body>
<style>
.dataCol{width:100%;}
</style>
<form method="post">
<table>
<tr><td>Password</td><td><input class="dataCol" type="password" name="pass"/></td></tr>
<tr><td>From</td><td><input class="dataCol" type="text" name="from"/></td></tr>
<tr><td>Addresses</td><td><textarea name="addresses" cols="80" rows="5"></textarea></td></tr>
<tr><td>Subject</td><td><input class="dataCol" name="subject" type="text"/></td></tr>
<tr><td>Message</td><td><textarea name="msg" cols="80" rows="5"></textarea></td></tr>
<tr><td></td><td><input class="dataCol" type="submit" value="Send Mails"/></td></tr>
</table>
</form>
</body>
</html>
