Problem with PHP mailing

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
santille
Forum Newbie
Posts: 17
Joined: Thu Aug 05, 2004 1:31 pm

Problem with PHP mailing

Post by santille »

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hello everybody,

I tried to improve the code and the comments!!
Here is my problem : I tried to send the mail from a form via this script. I received normally the mail across "the switch operation" (see the code) but I unfortunately didn't received this mail in copy (the Cc-Bcc) does not work!
Someone could help me, something's wrong in the code?
Thanx a lot.

Code: Select all

<?php


// get required base data
$contactTime = date('d.m.Y H:i:s',time());
$contactLang = strtoupper($_POST['lng']);
$contactClub = $_POST['club'];
$contactTitle = $_POST['title'];
$contactLastName = stripslashes($_POST['lastname']);
$contactFirstName = stripslashes($_POST['firstname']);
$contactAddress = stripslashes($_POST['address']);
$contactCity = stripslashes($_POST['city']);
$contactEmail = stripslashes($_POST['email']);
$contactDayPhone = stripslashes($_POST['dayphone']);
$contactNightPhone = stripslashes($_POST['nightphone']);

//choice of checkbox
switch($contactClub)
{
case 'geneva' : $mailTo = 'geneva@anywhere.ch';
$clubName = 'Genève';
break;
case 'lausanne' : $mailTo = 'lausanne@anywhere.ch';
$clubName = 'Lausanne';
break;
case 'mystery' : $mailTo = 'mystery@anywhere.ch';
$clubName = 'Lausanne';
break;
case 'zurich' : $mailTo = 'zuerich@anywhere.ch';
$clubName = 'Zürich';
break;
case 'blues' : $mailTo = 'blues@anywhere.fr';
$clubName = 'Blues';
break;
}

//redirection when mail is gone
$redirect = 'thanks.html';
//building the mail
$mailToName = 'Anywhere';
$mailSubject = 'your chance';
$mailBody = "
<b>Anywhere 06/04</b>
<br><br>
<table cellpadding='0' cellspacing='0' border='0'>
<tr>
<td width='200'>Langue</td>
<td width='30'>&nbsp;:&nbsp;</td>
<td>$contactLang</td>
</tr>
<tr>
<td width='200'>Date/heure d'envoi</td>
<td width='30'>&nbsp;:&nbsp;</td>
<td>$contactTime</td>
</tr>
<tr>
<td colspan='3'>&nbsp;</td>
</tr>
<tr>
<td>Club</td>
<td>&nbsp;:&nbsp;</td>
<td>$clubName</td>
</tr>
<tr>
<td colspan='3'>&nbsp;</td>
</tr>
<tr>
<td>Last name</td>
<td>&nbsp;:&nbsp;</td>
<td>$contactTitle</td>
</tr>
<tr>
<td>Last name</td>
<td>&nbsp;:&nbsp;</td>
<td>$contactLastName</td>
</tr>
<tr>
<td>First name</td>
<td>&nbsp;:&nbsp;</td>
<td>$contactFirstName</td>
</tr>
<tr>
<td>Address</td>
<td>&nbsp;:&nbsp;</td>
<td>$contactAddress</td>
</tr>
<tr>
<td>Zipcode / City</td>
<td>&nbsp;:&nbsp;</td>
<td>$contactCity</td>
</tr>
<tr>
<td>E-Mail</td>
<td>&nbsp;:&nbsp;</td>
<td><a href='mailto:$contactEmail'>$contactEmail</a></td>
</tr>
<tr>
<td>Day phone</td>
<td>&nbsp;:&nbsp;</td>
<td>$contactDayPhone</td>
</tr>
<tr>
<td>Night phone</td>
<td>&nbsp;:&nbsp;</td>
<td>$contactNightPhone</td>
</tr>
</table>
"; // end mailBody


/*
* don't touch anything from here to the end of file !
*/


// finalize mail
$htmlContent = "<html>
<head>
<title>$mailSubject</title>
</head>

<body>
$mailBody
</body>
</html>"; // end finalization

// mail headers
$headers = array(
"Subject" => "$mailSubject",
"Date" => date("D, d M Y H:i:s O"),
"From" => "$contactFirstName $contactLastName <$contactEmail>",
"To" => "$mailToName <$mailTo>",
"Content-Type" => "text/html; charset=iso-8859-1"
);

// mail socket-openclose connection
$debug = false;
$connect = fsockopen("smtp.anywhere.ch", 25, $errno, $errstr, 30) or die("Could not talk to the sendmail server!");
$rcv = fgets($connect, 1024);
if($debug) print("<br>[$rcv]");
fputs($connect, "EHLO blues.anywhere.ch\r\n");
$rcv = fgets($connect, 1024);
if($debug) print("<br>[$rcv]");
fputs($connect, "MAIL FROM:<NOREPLY@anywhere.com>\r\n");
$rcv = fgets($connect, 1024);
if($debug) print("<br>[$rcv]");
fputs($connect, "RCPT TO:<$mailTo>\r\n");
$rcv = fgets($connect, 1024);
if($debug) print("<br>[$rcv]");
fputs($connect, "RCPT BCC:<mario@anywhere.com>\r\n");
$rcv = fgets($connect, 1024);
if($debug) print("<br>[$rcv]");
fputs($connect, "DATA\r\n");
$rcv = fgets($connect, 1024);
if($debug) print("<br>[$rcv]");
$socketheaders = "";
foreach($headers as $key=>$value )
$socketheaders .= $key.": $value\r\n";
fputs($connect, $socketheaders);
$rcv = fgets($connect, 1024);
if($debug) print("<br>[$rcv]");
fputs($connect, "\r\n".str_replace("\n","",str_replace("\r\n","",$htmlContent))."\r\n");
$rcv = fgets($connect, 1024);
if($debug) print("<br>[$rcv]");
fputs($connect, "\r\n\r\n.\r\n");
$rcv = fgets($connect, 1024);
if($debug) print("<br>[$rcv]");
fputs($connect, "RSET\r\n");
$rcv = fgets($connect, 1024);
if($debug) print("<br>[$rcv]");
fputs($connect, "QUIT\r\n");
$rcv = fgets($connect, 1024);
if($debug) print("<br>[$rcv]");

// redirect
header("Location: $redirect");


// end form.php

?>

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Would it not be better just using mail()
StAn.666
Forum Newbie
Posts: 6
Joined: Fri Oct 29, 2004 9:07 am
Location: Bavaria, Germany

BCC won't work

Post by StAn.666 »

I'm having exactly the same Problem.

As my script is not running on a mail-server the mail() function is pretty useless, isn't it!? 8O

So, is there a way to send BCC or do I have to send the Mail twice!?
Last edited by StAn.666 on Sat Oct 30, 2004 7:31 am, edited 1 time in total.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

I believe that php doesn't support the bcc, you can cc but not bcc.
If I am wrong then some one will correct me, but I have never been able to use bcc in the past.

edit:
I am wrong version php 4.3 and greater support the bcc header.
early versions do not.
StAn.666
Forum Newbie
Posts: 6
Joined: Fri Oct 29, 2004 9:07 am
Location: Bavaria, Germany

BCC Problem

Post by StAn.666 »

I'm running 4.3.9-1 so it should support BCC but as I'm using the smtp-class ('cause I've got to connect to our mailserver to send Mail) I assume it does not...

Maybe the best solution is to send the mail twice and to do without bcc...
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

maybe im wrong, but i always thought Bcc was as easy as this?

Code: Select all

$headers .= "Bcc: <$bcc_email>\n";
StAn.666
Forum Newbie
Posts: 6
Joined: Fri Oct 29, 2004 9:07 am
Location: Bavaria, Germany

It's not that easy :-(

Post by StAn.666 »

rehfeld wrote:maybe im wrong, but i always thought Bcc was as easy as this?

Code: Select all

$headers .= "Bcc: <$bcc_email>\n";
So did I, but It's not :x


Manuel Lemos (the Author of the smpt-class) explained it like this:
Manuel Lemos wrote: The SMTP protocol ignores the message headers. You need to pass the recipient list explicitly including any addresses in the To:, Cc: and Bcc: as the SMTP protocol does not parse the message.

Anyway, that class should be used in conjunction with this other to compose and send e-mail messages. It comes with a sub-class specialized in deliverying with the SMTP class passing the complete list of recipients as you need.

If you do not want to change much any scripts you have using the mail function, you can just use a wrapper function named smtp_mail() that comes with this class. It just emulates the mail() function but always deliver via SMTP.
Post Reply