473,756 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using mail() for attachments

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
7 3273

"PeterR" <sp**@spam.co m> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ:
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.p hp');
include('Mail/mime.php');

$text = 'Text version of email';
$html = '<html><body>HT ML 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($te xt);
$mime->setHTMLBody($h tml);
$mime->addAttachment( $file, 'text/plain');

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

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

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

$text = 'Text version of email';
$html = '<html><body>HT ML 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($te xt);
$mime->addHTMLImage ($file, 'image/jpeg');
$mime->setHTMLBody($h tml);

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

Also you can send mail using sockets.
Jul 17 '05 #2
> 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_".md 5(rand());
$text_boundary = "----=_NextPart_".md 5(rand());
$html_boundary = "----=_NextPart_".md 5(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\tbound ary=\"$main_bou ndary\"\n";
$message = "\n--$main_boundary\ n";
$message .= "Content-Type:
multipart/alternative;\n\ tboundary=\"$te xt_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_mes sage":"Text portion of
HTML Email";
$message .= "\n--$text_boundary\ n";
$message .= "Content-Type:
multipart/related;\n\tbou ndary=\"$html_b oundary\"\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=\"$fil e_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.a t";
# Recipient Name
$to_name = "Ludwig Moser";
# Recipient Email
$to_email = "lu******@gmx.a t";
# 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><t itle></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><tab le 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.jp g\"><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

"Ludwig Moser" <l.*****@chello .at> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ:
news:M9******** *************@n ews.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_".md 5(rand());
$text_boundary = "----=_NextPart_".md 5(rand());
$html_boundary = "----=_NextPart_".md 5(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\tbound ary=\"$main_bou ndary\"\n";
$message = "\n--$main_boundary\ n";
$message .= "Content-Type:
multipart/alternative;\n\ tboundary=\"$te xt_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_mes sage":"Text portion of
HTML Email";
$message .= "\n--$text_boundary\ n";
$message .= "Content-Type:
multipart/related;\n\tbou ndary=\"$html_b oundary\"\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=\"$fil e_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.a t";
# Recipient Name
$to_name = "Ludwig Moser";
# Recipient Email
$to_email = "lu******@gmx.a t";
# 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><t itle></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><tab le 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.jp g\"><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
> 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

"Ludwig Moser" <l.*****@chello .at> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ:
news:Oo******** ************@ne ws.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
[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\tes t\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
> [mail function]
; For Win32 only.
SMTP = ip_of_your_mail _server


sorry, hehe
i edited the php ini ;)

Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2449
by: S_p_ike | last post by:
Hi all, I have a problem with mail with attachments. To send e-mail with attachments from a form, I use Setec Astronomy's libmail class (v 1.4) (available here http://www.phpclasses.org/) Everything is ok but if I use a specific e-mail address (say address X) in the field To, the e-mail arrives without attachments. (even if I use two addresses for field To, of which one is address X, the mail to address X arrives without attachments,...
5
7386
by: Colin Anderson | last post by:
I discovered, with great excitement, this article http://www.davison.uk.net/vb2notes.asp when researching methods for emailing from Access via Notes. Unfortunatly, when I run this I get a Run-time error. When I run it on an XP machine it crashes, but on an NT box it just generates an unknown error, handled by the error handler. I have debugged and stepped through the code and have narrowed the issue to the point at which the...
1
8235
by: Trond A. S. Andersen | last post by:
Hi all, I'm working on a project in which i need to send mail attachments in the form of Excel documents from a .NET C# application. I've written a class which creates the Excel document using the automation features provided by the Excel.Application component. The Excel workbooks/documents are stored locally before I call my SendExcelDocument() method in order to attatch each document to an instance of
3
5687
by: Tim T | last post by:
Hi, I hope there is someone reading this that has the answer, Please Help! I have the need to send a html email via asp.net. its easy enough to send an html email and add attachments. My question is, how to you set the Content-Location of each attachment in the mail headers so that the images are embedded in the html email rather than attached? This is my code:
2
10696
by: Landley | last post by:
Hello, Is there a way of creating an email, attaching a file and sending using the client's default mail client? I am looking for a none email client specific solution that does not involve specifying parameters such as mail servers etc. Cheers,
2
4532
by: Brad | last post by:
I have a web .Net app which sends emails with attachments. After the email is sent I clean up aftermyself and delete the attachments from disk. In upgrading to .Net 2 I changed the email logic from using system.web.mail to system.net.mail (of course vs is telling me system.web.mail is obsolete). In .Net 1.1, the files deleted with no problem but in .Net 2 when I attempt to delete I receive The process cannot access the file ...'...
7
4387
by: Ray Booysen | last post by:
Hi all I'm sending email via ASP.NET in HTML mode. Each email has exactly one attachment and I do have full access to the SMTP server. However, if I send the email in HTML format, the framework reports the following exception: "System.Web.HttpException: Could not access 'CDO.Message' object. ---> System.Reflection.TargetInvocationException: Exception has been thrown
0
3776
by: bambi | last post by:
I am facing issues while adding attachments in an E-mail via Mapisend. The code I use is below. If I try to send , I am getting error messgae in Mailmessage.12. Mailmessage.12 is expected as numeric, I do not know what should it be. If I Provide '0', it is throwing out error. Can any one help what shoudl I do to fix this. I am able to send E-mail sucessfully without Attachments.. Code (Please refer comments near Mailmessage.12) OPTIONS...
3
5954
by: zaxxon25 | last post by:
I am having an issue with attaching 2 attachments one xip and other excel file with email and sending using perl sendmail. I tried to look on various forums and everywhere i get advice using MIME :: Lite. Unfortunately i cannot use it . I have tried to write a program on my own to send 2 attachments and sending using perl sendmail but program is not working. It is reading only first file for attachment purposes and ignores second one.I would...
7
16920
by: mukeshrasm | last post by:
Hi I am no able to send mail and it is giving this error Warning: mail(): SMTP server response: 530 5.7.3 Client was not authenticated in c:\inetpub\wwwroot\eshop\includes\classes\email.php on line 522 and the code is: <?php /* $Id: email.php,v 1.12 2003/06/17 17:29:44 dgw_ Exp $
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.