CERN - European Organization for Nuclear Research     IT Division     IS Group  
       Saturday, 18 May 2024
Home Examples

5. "Sending mails from PHP script"'

Sending Mail from within your script is non-trivial and full of traps, so be aware that you have to know what you are doing, otherwise better leave it !!! 
 
In any case you might want to consider using the central Web-servers where advanced techniques like mailing forms etc can be done very easily !!! (for more info see http://cern.ch/WebServices/AuthoringDoc/Forms/forms.htm)

The simplest way is to use the 'mail' command of php:

mail("Andreas.Wagner@cern.ch", "The subject", "The message text here","From: some@domain.com" );
 


 

The 2 parts for sending mails from a Web-form using a PHP script are:

1) The form


<form enctype="multipart/form-data" method="post" action='sendmail.php'>
	<!-- See http://ch.php.net/features.file-upload for further reference -->
	<input type="hidden" name="MAX_FILE_SIZE" value="1000000"> 
	<table>
		<tr>
			<th colspan="2">Send mail</th>
		</tr>
		<tr>
			<th>To:</th>
			<td><input type='text' name='To' size="20"></td>
		</tr>
		<tr>
			<th>From:</th>
			<td><input type='text' name='From' size="20"></td>
		</tr>
		<tr>
			<th>Subject:</th>
			<td><input type='text' name='Subject' size="20"></td>
		</tr>
		<tr>
			<th>Body:</th>
			<td><textarea cols='20' rows='5' name='Body' rows="1" cols="20"></textarea></td>
		</tr>
		<tr>
			<th>Attachment:</th>
			<td><input name="userfile" type="file" size="20"></td>
		</tr>
		<tr>
			<td><input type='submit' value='Send' /></td>
			<td><input type='reset' /></td>
		</tr>
	</table>
</form>

2.) The PHP script

<?php

# Include files
include("Mail/mime.php");
include("Mail/Mail.php");

/***** Headers *****/
$from = $_REQUEST["From"];
$subject = $_REQUEST["Subject"];
$htmlbody = $_REQUEST["Body"];
$to = $_REQUEST["To"];

/***** Handle the attachment *****/
$tempfilename = $_FILES['userfile']['tmp_name'];
$filename = $_FILES["userfile"]["name"];
$filesize = $_FILES["userfile"]["size"];
$filetype = $_FILES["userfile"]["type"];

print "File name : " . $filename . "<br>";
print "File size : " . $filesize . "<br>";
print "File type : " . $filetype . "<br>";

# Since we want to support attachments, the sent mail will consist of 
# a html part and a mime part. Thus we use the class Mail_mime for 
# making sure that the mail is correctly assembled.

$mimeobj = new Mail_mime();
# This will be the html body
$mimeobj->setHTMLBody($htmlbody);

# Add the attachement if there is one
if ($filename != ""){
    if ( !empty($filetype) ) {
		$mimeobj->addAttachment($tempfilename,$filetype, $filename);
    }else {
		$mimeobj->addAttachment($tempfilename);
    }
    print "The attached file : " . $filename . "<br>";
}else { 
    print "Sent mail without attachment<br>";
}

# This is now the body containing a html-part and a mime-part.
$messagebody = $mimeobj->get();

# Get the mime-headers ( we will only use the content type from this array )
$mime_headers = $mimeobj->headers();

# The \n need to be there. If you would like to se why, remove them and 
# try to send a mail to yourself. Check the headers and you see why.
$headers = array("\nFrom"=>$from,
		 "\nSubject"=>$subject,
		 "\nContent-Type"=>$mime_headers['Content-Type']);

# We should now have enough information to let the Mail_Mail class assemble it 
# to a valid mail message.

# Using Mail_Mail::send($recipients, $headers, $body) to send the mail
Mail_Mail::send($to, $headers, $messagebody);

?>

Here you can see working example : send mail


Continue to: 6. "Using a PHP module that is not in the standard PHP distribution"