Jump to content

Sending HTML email with multiple attachments.


njdubois
Go to solution Solved by njdubois,

Recommended Posts

I have this code, Its working great!  Perfect!  Other than the emails are plain text and not HTML.  What can I do to this code, to make it send HTML instead of plain text?

 

$headers = "From: " . $MSG_row['from_email'];
 
// boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

// headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
 
// multipart boundary 
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_message . "\n\n"; 
$message .= "--{$mime_boundary}\n";
 
// preparing attachments
$main_path='email_upload/';

for($x=0;$x<count($file_name);$x++){
	$file = fopen($main_path.$file_name[$x],"rb");
	$data = fread($file,filesize($main_path.$file_name[$x]));
	fclose($file);
	$data = chunk_split(base64_encode($data));
	$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$file_name[$x]\"\n" . 
	"Content-Disposition: attachment;\n" . " filename=\"$file_name[$x]\"\n" . 
	"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
	$message .= "--{$mime_boundary}\n";
} 

// send 
$mail_sent = @mail($to, $MSG_row['msg_subject'], $message, $headers);

 

 

I find many examples of sending emails, and sending html emails, and emails with attachments.  I just can't find one with more than one attachment?

 

Thanks in advance for any and all help!

 

Nick

Link to comment
Share on other sites

you can use html in the content of the e-mail by adding the following headers:
 

// To send HTML mail, the Content-type header must be set
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

 cant help you with the multiple attachments, my knowledge is limited ;)

Link to comment
Share on other sites

Changing my headers to match yours causes the emails to come as garbage:

This is a multi-part message in MIME format. --==Multipart_Boundary_xf13a8b110e950e9c7f180b3e5f277c4ex Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit
tet to see if email is in html or formatted with attachment.
--==Multipart_Boundary_xf13a8b110e950e9c7f180b3e5f277c4ex Content-Type: {"application/octet-stream"}; name="golf thing.txt.txt" Content-Disposition: attachment; filename="golf thing.txt.txt" Content-Transfer-Encoding: base64 NjMwMjk1NTIwNw0KbWF1cmVlbg== --==Multipart_Boundary_xf13a8b110e950e9c7f180b3e5f277c4ex

 

This multiple attachment thing is tough!

 

Thanks for the help though!

 

Nick

Link to comment
Share on other sites

If you are wanting to send a multipart-mixed email where the email message is in HTML, and you have attachments; and there is no alternative to the HTML part, then Alkimuz was close. Leave the HEADERS as you had them in the first post, but use the HTML content type in the body:

 

 

 
// multipart boundary 
$message = "This is a multi-part message in MIME format.\n\n" . 
"--{$mime_boundary}\n" . 
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
"Content-Transfer-Encoding: 7bit\n\n" . $email_message . "\n\n"; 
$message .= "--{$mime_boundary}\n";

 

 

Here you are saying that $email_message is plain text. Change that Content-Type to "text/html".

 

Note: This will NOT magically change $email_message to an HTML formatted message. That variable needs to hold an HTML formatted text string.

Link to comment
Share on other sites

  • 7 months later...

Hi I want to upload 1 static file as an attachment (it never changes name or location how can i do this? as it will be an automatic script where a user enteres there email (hence the $e var)

 

 

 

 

can you maybe help me get this code working as im getting no file attached i can post my code if it helps but its a static file and named the same file in the same place all the time but it sends email but the attached file is missing from the email and i have no  idea why i have tryed so many different ways, i wish to not use phpmailer or swift i just want to use php if at all posible please help.
 

$random_hash = md5(date('r', time()));
$to = "$e";    
$from = "admin@jnetscripts.com";
$subject = 'J~Net SSL Certificate Notification!';
$message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>J~Net Message</title></head><body style="margin:0px; font-family:Tahoma, Geneva, sans-serif;"><div style="padding:10px; background:#333; font-size:24px; color:#CCC;"><a href="http://www.jnetscripts.com/premium/ssl_cirtificate.zip"><img src="http://www.jnetscripts.com/images/logojnet.png" width="36" height="30" alt="Click To Visit" style="border:none; float:left;"></a>   Hi there, You have a new Balance on J~Net  </div><div style="padding:24px; font-size:17px;"><br /><br />Hi '.$username.' Heres your SSL Certificates You Requested! Any Questions or issues please Contact Multimedia @ J~Net <p>  <a href="http://www.jnetscripts.com/multimedia</a><br /></div></body></html>';
$headers = "From: $from\n";
$header .= "Content-Type: multipart/mixed\n\n";
$header .= "name=\"ssl_cirtificate.zip\"\n\n";
$header .= "Content-Transfer-Encoding: base64\n\n";
$header .= "Content-Type: application/zip; name=\"ssl_cirtificate.zip\"\n\n";
$header .= "--$random_hash--";
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.