All in One PHP Mail Function- With Attachment file Functionality

Open Source 15 Jul , 2013  


Hi all, In our daily coding life, we used to apply mail function in our php code ,but sometimes it create problems when we need more featured function instead of simple mail() function provided by PHP, Today I am going to show you a php mail Function for easy Emailing.
It is using a PHPMailer Class file for it, which you can grab from here. I have made all the Emailing featured in PHP very easy to use by just calling a Single line function.

Here is the Code which you can grab for your own use –

function send_mail($from, $from_name, $to, $to_name, $subject, $body, $attachment="")

$mail = new PHPMailer();
$mail->From = $from;
$mail->FromName = $from_name;
$mail->Subject = $subject;
$search = array(
'@<script[^>]*?>.*?</script>@si',   // Strip out javascript
'@<[/!]*?[^<>]*?>@si',            // Strip out HTML tags
'@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
'@<![sS]*?--[ tnr]*>@'         // Strip multi-line comments

$altBody= preg_replace($search, '', $body);
$mail->AltBody = $altBody;
$mail->MsgHTML($body); $mail->AddAddress($to, $to_name);
if(!empty($attachment)) {



Arguments :

$from = Email Address of the person to show in "From" field
$from_name = Name of the person to show in "From" field
$to = Email address of the person to whom email will be sent
$to_name = Name of the person to whom email will be sent
$subject = Subject of the Email
$body = Email message text (html/plain text)
$attachment = Path to the Attachment file (optional)


So by this you can use that function to send emails very easily with you php code, Join us in facebook for more updates

, ,