Hi php folks,
If you would like to send a mail thru php, it is very easy,
You can send mail from online,
but If you want to send an attachment in php mail function, its somewhat difficult.
I have added some sample code to send an attachment in php mail
Create a file attachment.pdf which is going to be attached with the mail
And then use the following code in a php file
mailattachment.php
~~~~~~~~~~~~
<?php
$to = "toaddress@domain.com";
$subject = "Subject for mail attachment";
$random_hash = md5(date('r', time()));
$headers = "From: fromemail@domain.com\r\nReply-To: noreply@domain.com";
$headers .= "\r\nMIME-Version: 1.0";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents("attachment.pdf")));
$output = "--PHP-mixed-$random_hash;
Content-Type: text/plain; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
Hello World!
This is the simple text version of the email message.
--PHP-mixed-$random_hash
Content-Type: text/html; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is the <b>HTML</b> version of the email message.</p>
--PHP-mixed-$random_hash
Content-Type: application/pdf; name=attachment.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--";
echo @mail($to, $subject, $output, $headers);
?>
Change the content type to appropriate file type and make sure the attachment file is in correct location
example: for pdf Content-Type: application/pdf;
for gif : Content-Type: application/gif;
for text: Content-Type: application/plain;
Save the code in a file mailattachment.php and run it from the php server,
and check the inbox to see the attachment.
If you have any queries, please contact me thru mail
codesmokers@gmail.com
No comments:
Post a Comment