[solveds] sending text and html email - multipart, windows

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

[solveds] sending text and html email - multipart, windows

Post by batfastad »

Hi

I'm trying to send an html newsletter and having used an excellent windows mailing program called groupmail, for many years, I want to fire them off from our database (FileMaker), using PHP.

I have php 4.3.10 on windows 2000, apache 1.3.33

In php.ini I've set...

Code: Select all

їmail function]
; For Win32 only.
SMTP = smtp.myisp.net
smtp_port = 25

; For Win32 only.
sendmail_from = administrator@mydomain.com
This is the code I'm using from the example at
http://www.daniweb.com/techtalkforums/thread2959.html

Code: Select all

<?php
$boundary = "nextPart";

$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: Me <sales@mysite.com>\r\n";
$headers .= "Content-Type: multipart/alternative; boundary = $boundary\r\n";

//text version
$headers .= "\n--$boundary\n"; // beginning \n added to separate previous content
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "This is the plain version";

//html version
$headers .= "\n--$boundary\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "This is the <b>HTML</b> version";

$mailcommand = mail("batfastad@nowhere.net", "subject line", "", $headers);

if (!$mailcommand) {
	// E-MAIL NOT SENT
	echo ("mail not sent");
} else {
	// E-MAIL SENT SUCCESSFULLY
	echo ("sent!!!");
}
?>
The mail sends ok, but nothing appears in the body of the message in Thunderbird 1.x or Outlook XP.


I read that in php under windows the headers in the mail() function can only have a maximum size of 4096 bytes.
Obviously that's not ideal if I'm trying to send the body in the headers.

Is this true? Could this be causing my problem?
Is there a way to do this without sending the message body in the headers?


Also what do I do about the multipart boundary ($boundary above)?
Can it be anything so long as it's unique?
So some kind of timestamp with random characters should work, rather than $boundary = "nextpart"


Does anyone know why this isn't working??
I've read through some information at http://www.phpbuilder.com/columns/kartic20000807.php3 but i found it a bit heavy going.


Please help!!

Thanks guys

Ben
Last edited by batfastad on Mon Jun 20, 2005 9:10 am, edited 1 time in total.
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post by batfastad »

Just in case anyone was concerned, I got it working.

But not under windows.

It seems that more than one \n in the headers are stripped out leaving just one.

And you need two \n after the multipart boundaries, and the content-types.

Got it working on my ISP.

But using the mail() function to send many emails is unreliable as the mail socket keeps having to be opened and closed.

Out of 2000 messages, I got maybe to 30 before an error halted it.

So I went out looking for an alternative method and found phpmailer from http://phpmailer.sourceforge.net which is a free PHP class for sending mail.

Got it set up and tested it, it sent my 2000 messages straight away with great performance.
I'd configured phpmailer to use my ISPs sendmail program.

2000 messages in 310 seconds with 0 errors!!!


Hope this is of some use to someone.

Thanks

Ben
Post Reply