/    Sign up×
Community /Pin to ProfileBookmark

php mail function – diffferent subject based on the recipients

Hello everyone:

I am wondering if there is a way to use different suject line for different recipients.

I am using PHP mail function to send out email which confirm account registration. Email
will be send to students who registered an account, the lab assistant will also get an email
contain the same account registration information.

I want use different subject line for student and lab assistant.
Would it be possible to achieve this by using one mail function? Please give me some advises.

If you know some online resource discuss this topic please let me know. Thanks for your help!

PHP code I used for mail function.

[code=php]$mailto = “$email”;
$subject = “Computer Lab Account Registration Confirmation Email”;
$message = “”;
$under_gradAccount =””;
$gradAccounts = “”;
$gradAccount1 = “”;

/*
note: you need to include MIME type and content-type in order to
include message body fomatted with html tags.
Add multiple recipents in header portion
*/
$header .= “MIME-Version: 1.0n”;
$header .= “Content-type: text/html; charset=iso-8859-1n”;
$header .= “From: $fname $lname <$email>n”;
//$header.=”Cc:[email protected]”;
$header.=”Bcc:[email protected]”;

print (“First name: $fname. <br />”);
print (“Last name: $lname. <br />”);

print (“Gender: $gender. <br />”);

print(“Age: $age. <br />”);

print (“Department: $departments. <br />”);

print (“Email: $email. <br />”);

print (“Phone number: $phone_num. <br />”);

if($class_standing == “undergraduate”)
{
print “You are an $class_standing student.<br /> You applied for the following account(s):<br />”;

if(is_array($_POST[‘undergrad_account’]))
{
foreach($_POST[‘undergrad_account’] as $undergradAccount)
{
print (“$undergradAccount<br />”);
}
}

}

if($class_standing == “graduate”)
{
print (“You are a $class_standing student.<br /> You applied for the following account(s):<br />”);

if(is_array($_POST[‘undergrad_account’]))
{
foreach($_POST[‘undergrad_account’] as $undergradAccount)
{
print (“$undergradAccount<br />”);
}
}

if(is_array($_POST[‘grad_account’]))
{
foreach($_POST[‘grad_account’] as $gradAccount)
{
print (“$gradAccount<br />”);
}
}
}

$message =”<html><body>”;
$message .=”<h4>Please verify your account information.<br />If you have any questions, please contact <a href=”mailto:[email protected]”>Computer Lab Account Service</a>.</h4>”;
$message .=”<hr style=”color: orange; width: 800px;” />”;
$message .= “First name: $fname. <br />”;
$message .= “Last name: $lname. <br />”;
$message .= “Gender: $gender. <br />”;
$message .= “Age: $age. <br />”;
$message .= “Department: $departments. <br />”;
$message .= “Email: $email. <br />”;
$message .= “Phone: $phone_num. <br />”;

$message .= “You are a(n): $class_standing student. <br />”;
$message .= “You applied for the following accounts: <br />”;
//use implode function to convert array to string, so account option
//can be displayed in the email
if($class_standing == “undergraduate”)
{
if(is_array($_POST[‘undergrad_account’]))
{
$under_gradAccount .= implode(‘, ‘, $_POST[‘undergrad_account’]); //convert undergraduate account option from an array into a string
$message .= “–“. $under_gradAccount.”<br />”;
}

}

/*
convert graduate account into a string format
*/
if($class_standing == “graduate”)
{
//test to see if undergraduate checkbox group being checked
if(is_array($_POST[‘undergrad_account’]))
{
$gradAccount1 .= implode(‘, ‘, $_POST[‘undergrad_account’]);
}
else
$gradAccount1 = “”;

if(is_array($_POST[‘grad_account’]))
{
$gradAccounts .= implode(‘, ‘, $_POST[‘grad_account’]);
}

$message .= “–“.$gradAccount1 . ‘<br />’;
$message .= “–“.$gradAccounts.'<br />’;
}

if(empty($comments))
{
print “You did not enter any comments.”;
}

if(!empty($comments))
{
$message .= “Your comments on our account services are shown below:<br />”;
$message .= “‘ $comments'<br />”;
}

$message .= “The services you requested will be available once we approve your application.”;
$message .=”<hr style=”color: orange; width: 800px;” />”;

$message .= “<br /><br />Computer Lab Account Services<br />San Jose State University”;

$message .=”</body></html>”;

?>[/code]

webdev077
8-9-07

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@littlenedAug 10.2007 — I think you'll probably need to send 2 e-mails.

Hold the message, subject and email address in variables, then send the mail to the studay.

then assign a different subject to the variable and email and issue the mail send command again.
Copy linkTweet thisAlerts:
@knowjAug 10.2007 — I'm 99% sure it's got to be done with 2 emails if you want different subjects as email clients are ALOT more restrictive than browsers when it comes to code.

Also i would make the email a multipart as currently if the end user only accepts text emails there going to get a page full of rubbish.

just strip_tags() on the message before you pass it into the plain text part of the email.
×

Success!

Help @webdev077 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...