Hello everyone, I'm curious about something. I have a PHP Email script that I'm trying to get working and I think the code is right but just need a second pair of eyes to check it. It seems something in my if elseif else structure is hosed. The site does indeed email properly because I have each request followed by an autoresponder to the message sender. This is the block I'm stuck on: - if ($to=="sales@mydomain.com")
-
$subject="RFQ"
-
mail("1112223333@messaging.sprintpcs.com","","noreply@mydomain.com","You have received a site RFQ request.")
-
elseif ($to==webmaster@mydomain.com)
-
$subject="Website Related";
-
mail("1112223333@messaging.sprintpcs.com","","noreply@mydomain.com","You have received a site website request.")
-
elseif ($to==support@mydomain.com)
-
mail("1112223333@messaging.sprintpcs.com","","noreply@mydomain.com","You have received a site support request.")
-
$subject="Account support request";
-
else
-
$subject="Contact Request";
-
mail("1112223333@messaging.sprintpcs.com","","noreply@mydomain.com","You have received a site contact request.")
-
Not a problem, we're all new at some point.
If you get around to it, basically you would do something like this - $arr = array(
-
'John Smith' => array(
-
'Subject' => 'Hi',
-
'EMail' => 'John.Smith@domain.com'
-
)
-
'Jane Doe' => array(
-
'Subject' => 'Hello',
-
'EMail' => 'Jane.Doe@gmail.com'
-
)
-
);
-
-
$arrUser = $arr[$user];
-
sendMail($arrUser['Subject'], @arrUser['EMail']);
13 1753
Your if structure is completely off. You're missing a bunch of brackets and semicolons. The correct if structure is as follows - if (expression) {
-
statement;
-
} elseif (expression) {
-
statement;
-
} else {
-
statement;
-
}
I actually re-wrote this using case statements instead, way faster than if elseif else loop.
And here is the re-written code block -
$array = array('1'=>'info@mydomain.com',
-
'2'=>'support@mydomain.com',
-
'3'=>'sales@mydomain.com',
-
'4'=>'webmaster@mydomain.com');
-
-
if (isset($_POST['Dept']))
-
{
-
$to = $_POST['Dept'];
-
} else {
-
$to = 'default';
-
-
switch ($to) {
-
case 1:
-
$subject = "General Info Request";
-
$mailto = $array['1'];
-
break;
-
-
case 2:
-
$subject = "Account Support Request";
-
$mailto = $array['2'];
-
break;
-
-
case 3:
-
$subject = "Request For Quote from $name - $from";
-
$mailto = $array['3'];
-
mail("1112223333@vtext.com","","RFQ request from $name - $from waiting in your email.");
-
break;
-
-
case 4:
-
$subject = "Website assistance request from $name - $from";
-
$mailto = $array['4'];
-
break;
-
-
default:
-
$subject = "No department chosen - to catch-all.";
-
$mailto = "webmaster@mydomain.com";
-
break
-
}
-
Another option would be to store all the information in an array and then you can drop the case statements altogether.
Hmmm, I may just try that for fun. Right now, I just have to get it working :) Then I can experiment. You'll have to forgive me but I'm a little new to PHP. Most of my experience is in asp.net & C#. I am finding common threads between the languages though.
Not a problem, we're all new at some point.
If you get around to it, basically you would do something like this - $arr = array(
-
'John Smith' => array(
-
'Subject' => 'Hi',
-
'EMail' => 'John.Smith@domain.com'
-
)
-
'Jane Doe' => array(
-
'Subject' => 'Hello',
-
'EMail' => 'Jane.Doe@gmail.com'
-
)
-
);
-
-
$arrUser = $arr[$user];
-
sendMail($arrUser['Subject'], @arrUser['EMail']);
Ok, I'm trying to code this. The auto-reply to the user works fine but the first part where I send to different departments just doesn't work. Can someone help? -
<?php
-
$dept=$_REQUEST['Dept'];
-
$from=$_REQUEST['Email'];
-
$name=$_REQUEST['Name'];
-
$headers="From: noreply@mydomain.com";
-
$array = array('1'=>'info@mydomain.com','2'=>'support@mydomain.com','3'=>'sales@mydomain.com','4'=>'webmaster@mydomain.com');
-
if (isset($_POST['Dept']))
-
{
-
$to = $_POST['Dept'];
-
}
-
else
-
{
-
$to = 'default';
-
}
-
switch ($to)
-
{
-
case 1:
-
$subject = "General Info Request";
-
$mailto = $array['1'];
-
break;
-
case 2:
-
$subject = "Account Support Request";
-
$mailto = $array['2'];
-
break;
-
case 3:
-
$subject = "Request For Quote from $name - $from";
-
$mailto = $array['3'];
-
mail("1112223333@mytext.com","","RFQ request from $name - $from waiting in your email.");
-
break;
-
case 4:
-
$subject = "Website assistance request from $name - $from";
-
$mailto = $array['4'];
-
break;
-
default:
-
$subject = "No department chosen - to catch-all.";
-
$mailto = "webmaster@mydomain.com";
-
break;
-
}
-
$fields=array();
-
$fields{"Name"}="Name";
-
$fields{"Company"}="Company";
-
$fields{"Email"}="Email";
-
$fields{"Phone"}="Phone";
-
$fields{"list"}="Mailing List";
-
$fields{"Message"}="Message";
-
$body="We have received the following information:\n\n";
-
foreach($fields as $a => $b)
-
{
-
$body=sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
-
}
-
$headers2="From: noreply@mydomain.com";
-
$subject2="Thank you for contacting us";
-
$autoreply="Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 24 hours. If you have any more questions, please go to our website at www.mydomain.com";
-
if($from=='')
-
{
-
print "You have not entered an email, please go back and try again";
-
}
-
else
-
{
-
if($name=='')
-
{
-
print "You have not entered a name, please go back and try again";
-
}
-
else
-
{
-
$send=mail($mailto, $subject, $body, $headers);
-
$send2=mail($from, $subject2, $autoreply, $headers2);
-
if($send)
-
{
-
header("Location:http://www.mydomain.com/prototype/thankyou.html");
-
}
-
else
-
{
-
print "We encountered an error sending your mail, please notify webmaster@mydomain.com";
-
}
-
}
-
}
-
?>
-
It doesn't even send to default?
Nope, I just tried and no dice.
Actually, sales & support work. The info and webmaster one don't.
SO it would appear that case 1 & 4 do not work.
Here is another way you could set this up which I prefer but I know its a personal choice.
Also, I comments 2 parts that im not sure of.
#1: what are these fields supposed to be? are they supposed to be coming from the POST data from the form?
#2: the original foreach loop, overwrote $body with each iteration, im not sure why you want to loop through the fields but if you do and want to append to the $body variable, use " .= " instead of just " = ". -
<?php
-
$dept = $_REQUEST['Dept'];
-
$from = $_REQUEST['Email'];
-
$name = $_REQUEST['Name'];
-
-
$headers = "From: noreply@mydomain.com";
-
-
$mail_settings = array(
-
'1' => array(
-
'to' => 'info@mydomain.com',
-
'subject' => 'General Info Request'
-
),
-
'2' => array(
-
'to' => 'support@mydomain.com',
-
'subject' => 'Account Support Request'
-
),
-
'3' => array(
-
'to' => 'sales@mydomain.com',
-
'subject' => "Request For Quote from $name - $from"
-
),
-
'4' => array(
-
'to' => 'webmaster@mydomain.com',
-
'subject' => "Website assistance request from $name - $from"
-
),
-
'default' => array(
-
'to' => 'webmaster@mydomain.com',
-
'subject' => 'No department chosen - to catch-all.'
-
)
-
);
-
-
-
$to = (isset($_POST['Dept'])) ? $_POST['Dept'] : 'default';
-
-
$subject = $mail_settings[$to]['subject'];
-
$mailto = $mail_settings[$to]['to'];
-
-
$fields = array(); //#1
-
$fields["Name"] = "Name";
-
$fields["Company"] = "Company";
-
$fields["Email"] = "Email";
-
$fields["Phone"] = "Phone";
-
$fields["list"] = "Mailing List";
-
$fields["Message"] = "Message";
-
-
$body = "We have received the following information:\n\n";
-
-
foreach($fields as $a => $b){
-
-
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); //#2
-
-
}
-
-
$headers2 = "From: noreply@mydomain.com";
-
$subject2 = "Thank you for contacting us";
-
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 24 hours. ".
-
"If you have any more questions, please go to our website at www.mydomain.com";
-
-
if($from == ''){
-
-
print "You have not entered an email, please go back and try again";
-
-
}else{
-
-
if($name == ''){
-
-
print "You have not entered a name, please go back and try again";
-
-
}else{
-
-
$send = mail($mailto, $subject, $body, $headers);
-
$send2 = mail($from, $subject2, $autoreply, $headers2);
-
-
if($send){
-
-
header("Location:http://www.mydomain.com/prototype/thankyou.html");
-
-
}else{
-
-
print "We encountered an error sending your mail, please notify webmaster@mydomain.com";
-
-
}
-
}
-
}
-
?>
-
Its ok, I rewrote it and have it working now. I stripped out the foreach loop and just wrote the body out into the $body tag. Thanks :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Chris Lane |
last post by:
Need Advice on prebuilt Exception Assemblies Please take a look at my post on the Titled: Need Advice on prebuilt Exception Assemblies posted on 04/21/04
Thank
|
by: hazly |
last post by:
I'm very new in the web technology and need advice on search engine. I
want to develop a portal using PHP and MySQL on Linux. Need to know on
the following features :
1. search engine that could...
|
by: wayne |
last post by:
I have a PHP email script running on two separate websites. Today, I
received a form mail generated by the script from each site, with time
stamps 10 minutes apart. The entered email address in...
|
by: Jerim79 |
last post by:
I am no PHP programmer. At my current job I made it known that I was no
PHP programmer during the interview. Still they have given me a script
to write with the understanding that it will take me a...
|
by: ozzii |
last post by:
Hi,
I have a asp email script which uses cdosys to send emails with attahcments by iterating through a record set of 500 email addresses. However the script simply times out. I know you can...
|
by: Pete Marsh |
last post by:
Wondering if anyone can see an error with this script. I get a server
configuration error. THat could mean a module is not being loaded, but
maybe there's a syntax error here, can anyone spot it?...
|
by: harekrishna |
last post by:
Hiii
Can some please check my email script. I have gone through it several times but my mails won't come!!
here is the script
thank you-
.
.
|
by: SM |
last post by:
Hello,
I have a index.php template (2 columns). The right columns contains a
bunch of links (interviews, poems, etc...) The left columns contains
the actual article.
So if I click on a link on...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |