12 October, 2012 (published)
27 December, 2015 (last modified)

Complete php mail() script with To, ReplyTo, CC, BCC and attachments

  Categories: PHP
  Tags:  , , , , ,

Under certain circumstances one needs to send emails from a script. I am familiar with both Perl and php as script languages. These two script languages have a supporting community supplying all kinds of modules or packages (CPAN and PEAR). If you have those packages installed and they work you will not need this post.

However, installing those packages usually require a c compiler and the installation is not always that easy. A second complication is that you might want to run the script from a hoster, in which case you have no control over what packages are installed. In such a situation a script that only needs the core of the script language comes in quite handy.

In this post I will assume the php mail() function works. If it doesnot you will have to use a lower level script (using sockets). The latter will be treated in a separate post. The lower-level implementation is also used in our post-commit script.

Sending directly from a script requires some form of authentication  or otherwise the script could be used as a spamming script. In case of the php mail() function this is accomplished in the php.ini file configuraton file, a file that you cannot touch if it is on the hoster. If you run your script from a hoster the mail() function will have been implemented. The limitation being that you can only send emails using a from email address on the same domain.

Ou script implements:

  1. Adding a Reply-To address
  2. Adding one or more CC's
  3. Adding one or more BCC's
  4. Adding one or more attachments

How to use:

First: fill out replace all the necessary addresses and comment out those features you have not used in $header variable.

Second: replace names of attached files (if any). Copy those files to the script file location. Comment out  those parts of attachment part of the header you are not using.

Activate from browser.

</pre>
<?php

//
// copyright StringCat, Amsterdam 2012
//
/*
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

You should have received a copy of the GNU General Public License
 along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//
// simple email script using php mail with all possibilities:
// -multiple recipients
// -multiple cc
// -multiple bcc
// -specified reply-to address
// -attached file (s)
//

// just comment out a certain feature if you do not need it
// WARNING
// php mail() function only works if in the php.ini the smtp server is
// is set, from the domain where this script is invoked
// php mail() does not support smtp authemtification
//

//
// if you want to scale this script up, associative arrays should be used
// if you want to use less features, comment out what you do not want
//
// find location of script
$path_info = pathinfo(__FILE__);
$dir = $path_info['dirname'];
chdir($dir);

//
// subject , body
//
$emailSubject = 'Subject'; // replace with your own
$messageBody = 'This is the message'; // replace with your own
//
// sender
//
$fromEmailAddress = 'fromMe@example.com'; // replace with your own
$fromEmailName = 'My Name'; // replace with your own
$replyToEmailAddress = 'reply@domain.com'; // replace with your own
$replyToEmailName = 'MyReplyAddress'; // replace with your own
//
// recipients (add more if you need)
//
$toEmailAddress1 = 'recipient1@hisdomain.com'; // replace with your own
$toEmailName1 = 'firstRecipient'; // replace with your own
$toEmailFull1 = $toEmailName1 . ' <'. $toEmailAddress1 .'>';
$toEmailAddress2 = 'secondRecioient@herdomain.com'; // replace with your own
$toEmailName2 = 'secondRecipient'; // replace with your own
$toEmailFull2 = "$toEmailName2 <$toEmailAddress2>";
//
// cc's
//
$ccEmailAddress1 = 'cc1@cc1domain.com'; // replace with your own
$ccEmailName1 = 'first CC'; // replace with your own
$ccEmailAddress2 = 'cc2@cc2domain.com'; // replace with your own
$ccEmailName2 = 'second CC'; // replace with your own
//
// bcc's
//
$bccEmailAddress1 = 'bcc1@bcc1domain.com'; // replace with your own
$bccEmailName1 = 'first BCC'; // replace with your own
$bccEmailAddress2 = 'bcc2@bcc2domain.com'; // replace with your own
$bccEmailName2 = 'second BCC'; // replace with your own
//
// attachment 1
//
// replace with your own filename and mime-type
// file must be physically present at file location
// of this script
//
$attachmentShortFileName1 = "test.pdf"; // replace with your own
$attachmentFileName1 = $dir. "/".$attachmentShortFileName1;
$attachment1 = file_get_contents($attachmentFileName1);
$b64Attachment1 = chunk_split(base64_encode($attachment1));
$mimeType1 = "application/pdf"; // replace with your own

$attachmentShortFileName2 = "test.doc"; // replace with your own
$attachmentFileName2 = $dir. "/". $attachmentShortFileName2 ;
$attachment2 = file_get_contents($attachmentFileName2);
$b64Attachment2 = chunk_split(base64_encode($attachment2));
$mimeType2 = "application/msword"; // replace with your own

//
// headers input
//
// comment out if not used
//
$headers = "From: " . $fromEmailName . " <" . $fromEmailAddress . ">" . "\r\n" ;
$headers.= "Reply-To: " . $replyToEmailName . " <" . $replyToEmailAddress . ">" . "\r\n" ;
$headers.= "cc: " . $ccEmailName1 . " <" . $ccEmailAddress1 . ">" . "\r\n" ;
$headers.= "cc: " . $ccEmailName2 . " <" . $ccEmailAddress2 . ">" . "\r\n" ;
$headers.= "bcc: " . $bccEmailName1 . " <" . $bccEmailAddress1 . ">" . "\r\n" ;
$headers.= "bcc: " . $bccEmailName2 . " <" . $bccEmailAddress2 . ">" . "\r\n" ;

// Generate a mime boundary string
$rnd_str = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$rnd_str}x";
// Add headers for file attachment
$headers .= "MIME-Version: 1.0\n" .
 "Content-Type: multipart/mixed;\n" .
 " boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the plain message
$body = "This is a multi-part message in MIME format.\r\n\r\n" .
 "--{$mime_boundary}\r\n" .
 "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" .
 "Content-Transfer-Encoding: 7bit\r\n\r\n" .
 $messageBody . "\r\n\r\n";
// Add first file attachment to the message
$body .= "--{$mime_boundary}\r\n" .
 "Content-Type: {$mimeType1};\r\n" .
 " name=\"{$attachmentShortFileName1}\"\r\n" .
 "Content-Disposition: attachment;\r\n" .
 " filename=\"{$attachmentShortFileName1}\"\r\n" .
 "Content-Transfer-Encoding: base64\r\n\r\n" .
 $b64Attachment1 . "\r\n\r\n" .
//
// comment out one of the two following lines
// dependent on number of attached files
// last mime boundary must end in "--"
//
 "--{$mime_boundary}\r\n";
 // "--{$mime_boundary}--\r\n";
//
// Add second file attachment to the message
$body .="Content-Type: {$mimeType2};\r\n" .
 " name=\"{$attachmentShortFileName2}\"\r\n" .
 "Content-Disposition: attachment;\r\n" .
 " filename=\"{$attachmentShortFileName2}\"\r\n" .
 "Content-Transfer-Encoding: base64\r\n\r\n" .
 $b64Attachment2 . "\r\n\r\n" .
//
// comment out one of the two following lines
// dependent on number of attached files
// last mime boundary must end in "--"
//
 // "--{$mime_boundary}\r\n";
 "--{$mime_boundary}--\r\n";

$return = mail( "$toEmailFull1, $toEmailFull2 " , $emailSubject, $body, $headers );
// catch return code if you can do something useful with it
?>
<pre>

The file can also directly downloaded here.

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

4 reactions op “Complete php mail() script with To, ReplyTo, CC, BCC and attachments”

  1. sandeep says:

    how can i send mail from localhost

  2. Anahit says:

    Thank you very much for your code!It really helped me much!THANK YOU!

  3. Sash Raven says:

    Amazing.
    Thank you for your work and GNU License.

  4. Ken says:

    Wonderful example on using message string. Thank you so very much!

Write a reaction

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution.