Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Add Attachement To Email Function

Posted on 2008-06-09
2
Medium Priority
?
198 Views
Last Modified: 2008-06-10
Hi, I have attached the code to my email function.

I am having problems modifying it so that I can add attachments.
 
Can anyone help me out here?
function my_mail($to, $subject, $message, $from="", $charset="", $headers="", $html_email=0, $cc_admin=0)
	{
		global $db, $mybb, $lang;
	
		if(empty($charset))
		{
			$charset = "UTF-8";
		}
	
		// Build mail headers
		if (empty($from))
		{
			$from = "\"Graphix Labels\" <jsette@enterit.com.au>";
		}
		
		$headers .= "From: {$from}\n";
		$headers .= "Return-Path: {jsette@enterit.com.au}\n";
		
		if($_SERVER['SERVER_NAME'])
		{
			$http_host = $_SERVER['SERVER_NAME'];
		}
		else if($_SERVER['HTTP_HOST'])
		{
			$http_host = $_SERVER['HTTP_HOST'];
		}
		else
		{
			$http_host = "unknown.local";
		}
		
		// Changed for HTML format
		$headers .= "Message-ID: <". md5(uniqid(time()))."@{$http_host}>\n";
		$headers .= "MIME-Version: 1.0\n";
		
		if ($html_email == 1){
			$headers .= "Content-Type: text/html; charset=\"{$charset}\"\n";
		}
		else
		{
			$headers .= "Content-Type: text/plain; charset=\"{$charset}\"\n";
		}
		
		
		$headers .= "Content-Transfer-Encoding: 8bit\n";
		$headers .= "X-Priority: 3\n";
		$headers .= "X-MSMail-Priority: Normal\n";
		$headers .= "X-Mailer: MyBB\n";
		
		//if(defined("IN_ADMINCP"))
		//{
		//	$_SERVER['PHP_SELF'] = str_replace($mybb->config['admin_dir']."/", "admin-", $_SERVER['PHP_SELF']);
		//}
		
		$headers .= "X-MyBB-Script: {$http_host}{$_SERVER['PHP_SELF']}\n";
		
		if ($cc_admin == 1 ){
			$headers .= "Bcc: jsette@enterit.com.au \n"; // BCC to Enter IT (for testing purposes)
		}
	
		// For some reason sendmail/qmail doesn't like \r\n
		$sendmail = @ini_get('sendmail_path');
		if($sendmail)
		{
			$headers = preg_replace("#(\r\n|\r|\n)#s", "\n", $headers);
			$message = preg_replace("#(\r\n|\r|\n)#s", "\n", $message);
		}
		else
		{
			$headers = preg_replace("#(\r\n|\r|\n)#s", "\r\n", $headers);
			$message = preg_replace("#(\r\n|\r|\n)#s", "\r\n", $message);
		}
	
		mail($to, $subject, $message, $headers);
	}

Open in new window

0
Comment
Question by:jset_expert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 1400 total points
ID: 21748662
To use attachments use a PHP mailer class which can do that and will save you days of work. I recommend
http://phpmailer.sf.net
http://swiftmailer.sf.net
Easy to install and to use.  Recoding the attachment part for mails is very complicated and typical full of bugs. So don't write this part by your own. Use those well tested and working classes.
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 21749348
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question