Add Attachement To Email Function

Posted on 2008-06-09
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;




			$charset = "UTF-8";



		// Build mail headers

		if (empty($from))


			$from = "\"Graphix Labels\" <>";



		$headers .= "From: {$from}\n";

		$headers .= "Return-Path: {}\n";




			$http_host = $_SERVER['SERVER_NAME'];


		else if($_SERVER['HTTP_HOST'])


			$http_host = $_SERVER['HTTP_HOST'];




			$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";




			$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";




		//	$_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: \n"; // BCC to Enter IT (for testing purposes)



		// For some reason sendmail/qmail doesn't like \r\n

		$sendmail = @ini_get('sendmail_path');



			$headers = preg_replace("#(\r\n|\r|\n)#s", "\n", $headers);

			$message = preg_replace("#(\r\n|\r|\n)#s", "\n", $message);




			$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

Question by:jset_expert
LVL 48

Accepted Solution

hernst42 earned 350 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
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.
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 21749348

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now