By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,721 Members | 1,468 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,721 IT Pros & Developers. It's quick & easy.

using mail() for attachments

P: n/a
I've spent weeks trying to write a function that is simple to use for
sending emails with attachments, but I my programming skills are not that
advanced. I've noticed a few postings in these newsgroups with the same
problem, so I just want to share my solution.

I recommend using libmail from
http://lwest.free.fr/doc/php/lib/ind...e=mail&lang=en

it has made my university project much simplier now! :-)

--
Peter, Sunderland University. UK

PS: sorry for cross-posting.
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"PeterR" <sp**@spam.com> сообщил/сообщила в новостях следующее:
news:40828867.0@entanet...
I've spent weeks trying to write a function that is simple to use for
sending emails with attachments, but I my programming skills are not that
advanced. I've noticed a few postings in these newsgroups with the same
problem, so I just want to share my solution.

I recommend using libmail from
http://lwest.free.fr/doc/php/lib/ind...e=mail&lang=en

it has made my university project much simplier now! :-)

--
Peter, Sunderland University. UK

PS: sorry for cross-posting.


If you use Mail.php and Mail_mime.php packages from pear.php.net, there is
no any problem to send mail.

with attachment
<?php
include('Mail.php');
include('Mail/mime.php');

$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = '/home/richard/example.php';
$crlf = "\r\n";
$hdrs = array(
'From' => 'y**@yourdomain.com',
'Subject' => 'Test mime message'
);

$mime = new Mail_mime($crlf);

$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$mail =& Mail::factory('mail');
$mail->send('postmaster@localhost', $hdrs, $body);
?>

with picture
<?php
include('Mail.php');
include('Mail/mime.php');

$text = 'Text version of email';
$html = '<html><body>HTML version of email<img
src="image.jpg"></body></html>';
$file = '/tmp/image.jpg';
$crlf = "\r\n";
$hdrs = array(
'From' => 'y**@yourdomain.com',
'Subject' => 'Test mime message'
);

$mime = new Mail_mime($crlf);

$mime->setTXTBody($text);
$mime->addHTMLImage ($file, 'image/jpeg');
$mime->setHTMLBody($html);

$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('mail');
$mail->send('postmaster@localhost', $hdrs, $body);
?>

Also you can send mail using sockets.
Jul 17 '05 #2

P: n/a
> If you use Mail.php and Mail_mime.php packages from pear.php.net, there is
no any problem to send mail.
i do not have pear installed so i do not have those files

i tried to figure it out, no errors come up, but no mail is sent in this
code:

<?php
function sendmail ($from_name, $from_email, $to_name, $to_email,
$subject, $text_message="", $html_message="", $attachment="")
{
$from = "$from_name <$from_email>";
$to = "$to_name <$to_email>";
$main_boundary = "----=_NextPart_".md5(rand());
$text_boundary = "----=_NextPart_".md5(rand());
$html_boundary = "----=_NextPart_".md5(rand());
$headers = "From: $from\n";
$headers .= "Reply-To: $from\n";
$headers .= "X-Mailer: The Bat! (v2.04.7) Personal\n"; // say we are
the bat mail client ;-)
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type:
multipart/mixed;\n\tboundary=\"$main_boundary\"\n";
$message = "\n--$main_boundary\n";
$message .= "Content-Type:
multipart/alternative;\n\tboundary=\"$text_boundary\"\n";
$message .= "\n--$text_boundary\n";
$message .= "Content-Type: text/plain; charset=\"ISO-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 7bit\n\n";
$message .= ($text_message!="")?"$text_message":"Text portion of
HTML Email";
$message .= "\n--$text_boundary\n";
$message .= "Content-Type:
multipart/related;\n\tboundary=\"$html_boundary\"\n";
$message .= "\n--$html_boundary\n";
$message .= "Content-Type: text/html; charset=\"ISO-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n\n";
$message .= str_replace ("=", "=3D", $html_message)."\n";
if (isset ($attachment) && $attachment != "" && count ($attachment)= 1)

{
for ($i=0; $i<count ($attachment); $i++)
{
$attfile = $attachment[$i];
$file_name = basename ($attfile);
$fp = fopen ($attfile, "r");
$fcontent = "";
while (!feof ($fp))
{
$fcontent .= fgets ($fp, 1024);
}
$fcontent = chunk_split (base64_encode($fcontent));
@fclose ($fp);
$message .= "\n--$html_boundary\n";
$message .= "Content-Type: application/octetstream\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: inline;
filename=\"$file_name\"\n";
$message .= "Content-ID: <$file_name>\n\n";
$message .= $fcontent;
}
}
$message .= "\n--$html_boundary--\n";
$message .= "\n--$text_boundary--\n";
$message .= "\n--$main_boundary--\n";
@mail ($to, $subject, $message, $headers);
}
# Example
# Sender Name
$from_name = "ich bin ich";
# Sender Email
$from_email = "lu******@gmx.at";
# Recipient Name
$to_name = "Ludwig Moser";
# Recipient Email
$to_email = "lu******@gmx.at";
# Email Subject
$subject = "von mir an mich";
# Text Portion
$text_message = "This is HTML email and your email client softawre ain't
support HTML email.";
# HTML Portion
$html_message = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0
Transitional//EN\">\n";
$html_message.= "<html><head><title></title>\n";
$html_message.= "<meta http-equiv=\"Content-Type\" content=\"text/html;
charset=iso-8859-1\">\n";
$html_message.= "<style type=\"text/css\">\n";
$html_message.= "body, td {\nfont-family: Trebuchet MS;\nfont-size:
12px;\n}\n";
$html_message.= "</style>\n";
$html_message.= "</head>\n";
$html_message.= "<body><table width=\"100%\" cellpadding=0
cellspacing=0><tr valign=top align=center>";
$html_message.= "<td width=300>This is my HTML Body\n<br>";
$html_message.= "<img src=\"cid:hh.jpg\"><br>\n";
$html_message.= "<font color=red>nice button heh?.";
$html_message.= "</td>";
$html_message.= "</tr></table>";
$html_message.= "</body></html>";

# Attachment Location
$attachment = array ("./go.gif"); // image in the same folder
# Execute SendMail Function
sendmail ($from_name, $from_email, $to_name, $to_email, $subject,
$text_message, $html_message, $attachment);
echo "mail sent.";
?>

any ideas?

my system is running on php4.3, apache2 on windowsXP

TIA
Luke
Jul 17 '05 #3

P: n/a

"Ludwig Moser" <l.*****@chello.at> сообщил/сообщила в новостях следующее:
news:M9*********************@news.chello.at...
If you use Mail.php and Mail_mime.php packages from pear.php.net, there is no any problem to send mail.
i do not have pear installed so i do not have those files

i tried to figure it out, no errors come up, but no mail is sent in this
code:

<?php
function sendmail ($from_name, $from_email, $to_name, $to_email,
$subject, $text_message="", $html_message="", $attachment="")
{
$from = "$from_name <$from_email>";
$to = "$to_name <$to_email>";
$main_boundary = "----=_NextPart_".md5(rand());
$text_boundary = "----=_NextPart_".md5(rand());
$html_boundary = "----=_NextPart_".md5(rand());
$headers = "From: $from\n";
$headers .= "Reply-To: $from\n";
$headers .= "X-Mailer: The Bat! (v2.04.7) Personal\n"; // say we

are the bat mail client ;-)
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type:
multipart/mixed;\n\tboundary=\"$main_boundary\"\n";
$message = "\n--$main_boundary\n";
$message .= "Content-Type:
multipart/alternative;\n\tboundary=\"$text_boundary\"\n";
$message .= "\n--$text_boundary\n";
$message .= "Content-Type: text/plain; charset=\"ISO-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 7bit\n\n";
$message .= ($text_message!="")?"$text_message":"Text portion of
HTML Email";
$message .= "\n--$text_boundary\n";
$message .= "Content-Type:
multipart/related;\n\tboundary=\"$html_boundary\"\n";
$message .= "\n--$html_boundary\n";
$message .= "Content-Type: text/html; charset=\"ISO-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n\n";
$message .= str_replace ("=", "=3D", $html_message)."\n";
if (isset ($attachment) && $attachment != "" && count ($attachment)
= 1)

{
for ($i=0; $i<count ($attachment); $i++)
{
$attfile = $attachment[$i];
$file_name = basename ($attfile);
$fp = fopen ($attfile, "r");
$fcontent = "";
while (!feof ($fp))
{
$fcontent .= fgets ($fp, 1024);
}
$fcontent = chunk_split (base64_encode($fcontent));
@fclose ($fp);
$message .= "\n--$html_boundary\n";
$message .= "Content-Type: application/octetstream\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: inline;
filename=\"$file_name\"\n";
$message .= "Content-ID: <$file_name>\n\n";
$message .= $fcontent;
}
}
$message .= "\n--$html_boundary--\n";
$message .= "\n--$text_boundary--\n";
$message .= "\n--$main_boundary--\n";
@mail ($to, $subject, $message, $headers);
}
# Example
# Sender Name
$from_name = "ich bin ich";
# Sender Email
$from_email = "lu******@gmx.at";
# Recipient Name
$to_name = "Ludwig Moser";
# Recipient Email
$to_email = "lu******@gmx.at";
# Email Subject
$subject = "von mir an mich";
# Text Portion
$text_message = "This is HTML email and your email client softawre

ain't support HTML email.";
# HTML Portion
$html_message = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0
Transitional//EN\">\n";
$html_message.= "<html><head><title></title>\n";
$html_message.= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
$html_message.= "<style type=\"text/css\">\n";
$html_message.= "body, td {\nfont-family: Trebuchet MS;\nfont-size:
12px;\n}\n";
$html_message.= "</style>\n";
$html_message.= "</head>\n";
$html_message.= "<body><table width=\"100%\" cellpadding=0
cellspacing=0><tr valign=top align=center>";
$html_message.= "<td width=300>This is my HTML Body\n<br>";
$html_message.= "<img src=\"cid:hh.jpg\"><br>\n";
$html_message.= "<font color=red>nice button heh?.";
$html_message.= "</td>";
$html_message.= "</tr></table>";
$html_message.= "</body></html>";

# Attachment Location
$attachment = array ("./go.gif"); // image in the same folder
# Execute SendMail Function
sendmail ($from_name, $from_email, $to_name, $to_email, $subject,
$text_message, $html_message, $attachment);
echo "mail sent.";
?>

any ideas?

my system is running on php4.3, apache2 on windowsXP

TIA
Luke


If you have installed pear module correctlly so it maybe problem with Apache
2. http://php.net/install.apache2
Jul 17 '05 #4

P: n/a
> If you have installed pear module correctlly so it maybe problem with
Apache
2. http://php.net/install.apache2


i do not have pear installed...
php.net says:
Installation
There is no installation needed to use these functions; they are
part of the PHP core.

sorry my fault, no smtp server installed

any recommendations for a smtp server (freeware)

TIA
Luke
Jul 17 '05 #5

P: n/a

"Ludwig Moser" <l.*****@chello.at> сообщил/сообщила в новостях следующее:
news:Oo********************@news.chello.at...
If you have installed pear module correctlly so it maybe problem with

Apache
2. http://php.net/install.apache2


i do not have pear installed...
php.net says:
Installation
There is no installation needed to use these functions; they are
part of the PHP core.

sorry my fault, no smtp server installed

any recommendations for a smtp server (freeware)

TIA
Luke

[mail function]
; For Win32 only.
SMTP = ip_of_your_mail_server

your should indicate ip_of_your_mail_server. It can be a mail server of your
provider or any other mail server that accept mail from you.
Jul 17 '05 #6

P: n/a
[mail function]
; For Win32 only.
SMTP = ip_of_your_mail_server your should indicate ip_of_your_mail_server. It can be a mail server of your provider or any other mail server that accept mail from you.


added
# For Win32 only.
SMTP = 127.0.0.1;

then php reports
Parse error: parse error, unexpected '=' in D:\httproot\test\sendmail.php on
line 55

i have now installed PostCast a freeware SMTP server, its up and working
localhost:25

TIA
Luke
Jul 17 '05 #7

P: n/a
> [mail function]
; For Win32 only.
SMTP = ip_of_your_mail_server


sorry, hehe
i edited the php ini ;)

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.