Saturday, May 21, 2016

How To Send An Email From A PHP Script With Attachments

PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipients email address, the subject of the the message and the actual message additionally there are other two optional parameters.

mail( to, subject, message, headers, parameters );


Here is the description for each parameters.

Parameter Description
to  Specifies the receiver / receivers of the email
subject  Specifies the subject of the email. This parameter cannot contain any newline characters
message  Defines the message to be sent. Each line should be separated with a LF ( ). Lines should not exceed 70 characters

As soon as the mail function is called PHP will attempt to send the email then it will return true if successful or false if it is failed.

Multiple recipients can be specified as the first argument to the mail() function in a comma separated list.

Example

<?php
$to = "recipient@example.com";
$subject = "Hi!";
$body = "Hi, How are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>


Thats it! Note that you can have PHP validate your email addresses for correctness before sending.


Sending Attachments - PHP Email Script

To send an email with mixed content requires to set Content-type header to multipart/mixed. Then text and attachment sections can be specified within boundaries.

A boundary is started with two hyphens followed by a unique number which can not appear in the message part of the email. A PHP function md5() is used to create a 32 digit hexadecimal number to create unique number. A final boundary denoting the emails final section must also end with two hyphens.

Attached files should be encoded with the base64_encode() function for safer transmission and are best split into chunks with the chunk_split() function. This adds inside the file at regular intervals, normally every 76 characters.

Following is the example which will send a file /php/phpmanual.txt as an attachment. you can code your program to receive an uploaded file and send it.
<?php
$to = "xyz@examplemail.com";
$subject = "This is subject";
$message = "This is test message.";
# Open a file
$file = fopen( "/php/phpmanual.txt", "r" );
if( $file == false )
{
echo "Error in opening file";
exit();
}
# Read the file into a variable
$size = filesize("/php/phpmanual.txt");
$content = fread( $file, $size);

# encode the data for safe transit
# and insert after every 76 chars.
$encoded_content = chunk_split( base64_encode($content));

# Get a random 32 bit number using time() as seed.
$num = md5( time() );

# Define the main headers.
$header = "From:xyz@examplemail.com ";
$header .= "MIME-Version: 1.0 ";
$header .= "Content-Type: multipart/mixed; ";
$header .= "boundary=$num ";
$header .= "--$num ";

# Define the message section
$header .= "Content-Type: text/plain ";
$header .= "Content-Transfer-Encoding:8bit ";
$header .= "$message ";
$header .= "--$num ";

# Define the attachment section
$header .= "Content-Type: multipart/mixed; ";
$header .= "name="phpmanual.txt" ";
$header .= "Content-Transfer-Encoding:base64 ";
$header .= "Content-Disposition:attachment; ";
$header .= "filename="phpmanual.txt" ";
$header .= "$encoded_content ";
$header .= "--$num--";

# Send email now
$retval = mail ( $to, $subject, "", $header );
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Error Sending Message...";
}
?>

 Thats all! You can implement this php script in your web page with some minor changes and you will be able to send emails using it.Isnt it interesting?

If you like this post then you can Subscribe to our blog to receive similar interesting updates directly in your email.You can use the Subscription Box below to subscribe to all our future updates for free.

Enjoy!