472,336 Members | 1,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,336 software developers and data experts.

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 3167

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

"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
> 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********************@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
[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
> [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
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)...
5
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...
1
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...
3
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...
2
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...
2
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. ...
7
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,...
0
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...
3
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...
7
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.