Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sending email with attachement with mail() and local mailserver

0 views
Skip to first unread message

Jivanmukta

unread,
Dec 27, 2009, 9:31:51 AM12/27/09
to
Hello,
I am learnimg PHP5. I am trying to send an email with attachment. I
decided to use mail() function with mailserver installed (Hamster in
WinXP) . The problem is that I don't receive email although is see in
Hamster's event log that it was sent. I would like to use mail()
function because I had problems with PHPMailer and PEAR.
I use the following function. BTW is this correct?

function sendEmail($recipients, $subject, $bodyText, $bodyHTML,
$fullNo, $withAttachment, $logEntry) {
global $db, $websiteRoot;
$randomHash = md5(date('r', time()));
$headers = "From: test@localhost\r\nReply-To: test@localhost";
$headers .= 'Bcc: ';
foreach (explode(',', $recipients) as $bcc) {
$headers .= $bcc . ',';
}
$headers = rtrim($headers, ',');
$headers .= "\r\n";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=
\"PHP-alt-" . $randomHash . "\"";
if (!empty($fullNo)) {
getYearMonthNo($fullNo, $year, $month, $no);
}
$message = <<< EOM
--PHP-mixed-$randomHash
Content-Type: multipart/alternative; boundary="PHP-alt-$randomHash"

--PHP-alt-$randomHash
Content-Type: text/plain; charset="iso-8859-2"
Content-Transfer-Encoding: 7bit

$bodyText

--PHP-alt-$randomHash
Content-Type: text/html; charset="iso-8859-2"
Content-Transfer-Encoding: 7bit

$bodyHTML

--PHP-alt-$randomHash--

EOM;
if ($withAttachment) {
$result = $db->query('SELECT file_name, picture FROM
pictures ' .
"WHERE a_year = $year AND a_month = $month
AND a_no = $no");
@mkdir($websiteRoot . '/tmp/' . $fullNo);
while ($row = $result->fetch_row()) {
createPicture($websiteRoot . '/tmp', $fullNo,
$row[0], $row[1]);
$attachment = chunk_split(base64_encode
(file_get_contents("$websiteRoot/tmp/$fullNo/$row[0]")));
$message .= <<< EOM
--PHP-mixed-$randomHash
Content-Type: application/octet-stream; name="$row[0]"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$attachment
--PHP-mixed-$randomHash--

EOM;
}
$result->close();
removeDirectory($websiteRoot . '/tmp/' . $fullNo);
}
$mailSent = @mail('', $subject, $message, $headers);
if ($mailSent) {
if (!empty($fullNo)) {
writeToLog(null, null, null, $year, $month,
$no, $logEntry);
} else {
writeToLog(null, null, null, null, null, null,
$logEntry);
}
}
return $mailSent;
}

Doug Miller

unread,
Dec 27, 2009, 9:53:39 AM12/27/09
to
In article <36bec512-afde-4444...@r24g2000yqd.googlegroups.com>, Jivanmukta <jivan...@poczta.onet.pl> wrote:
>Hello,
>I am learnimg PHP5. I am trying to send an email with attachment. I
>decided to use mail() function with mailserver installed (Hamster in
>WinXP) . The problem is that I don't receive email although is see in
>Hamster's event log that it was sent. I would like to use mail()
>function because I had problems with PHPMailer and PEAR.
>I use the following function. BTW is this correct?

No.


>
>function sendEmail($recipients, $subject, $bodyText, $bodyHTML,
>$fullNo, $withAttachment, $logEntry) {
> global $db, $websiteRoot;
> $randomHash = md5(date('r', time()));
> $headers = "From: test@localhost\r\nReply-To: test@localhost";
> $headers .= 'Bcc: ';

You need a \r\n delimiter between the Reply-To: and Bcc: headers. As you have
it here, $headers will contain
From:test@localhost
Reply-To: test@localhostBcc: <etc>

> foreach (explode(',', $recipients) as $bcc) {
> $headers .= $bcc . ',';
> }

This will leave a comma after the last Bcc: email address.

Haven't looked at the rest, but you need to fix those problems at least before
it's going to work.

C. (http://symcbean.blogspot.com/)

unread,
Dec 27, 2009, 6:56:44 PM12/27/09
to
On 27 Dec, 14:31, Jivanmukta <jivanmu...@poczta.onet.pl> wrote:
> Hello,
> I am learnimg PHP5. I am trying to send an email with attachment. I
> decided to use mail() function with mailserver installed (Hamster in
> WinXP) . The problem is that I don't receive email although is see in
> Hamster's event log that it was sent. I would like to use mail()
> function because I had problems with PHPMailer and PEAR.
> I use the following function. BTW is this correct?
>
<snip lots of code>

No. We have better things to do than debug your code for you.

Have you tested the MTA using a conventional MUA?

If so, and it works as expected, try stripping your code down to the
minimum necessary to replicate the fault. If you still don't
understand why its not working, post again with the minimum code and
the corresponding log entries from your MTA.

C.

0 new messages