Solved

phpmailer mutliple emails sent

Posted on 2010-09-13
9
382 Views
Last Modified: 2012-05-10
Hi, I am trying to figure out and learn how to use phpmailer. What I am trying to accomplish is having a form sent to us and then a different html email sent as confirmation to the person who filled out the form. Could someone look over my code and tell me why the second code is not sending the email. We are receiving the first email but the confirmation email is not going out. Thank you for your help.
require_once('../contact/phpmailer/class.phpmailer.php');

							

							$fromname = stripslashes(trim($_POST['name']));

							$fromemail = stripslashes(trim($_POST['email']));

							$phone = stripslashes(trim($_POST['phone']));

							$mailing = stripslashes(trim($_POST['mailing']));

							$city = stripslashes(trim($_POST['city']));

							$state = stripslashes(trim($_POST['state']));

							$zip = stripslashes(trim($_POST['zip']));

							$start = stripslashes(trim($_POST['start']));

							$end = stripslashes(trim($_POST['end']));

							$hash = md5(uniqid("up_",true).$_POST['name']);

							$subject = "[TV Weekly magazine] - Vacation Stop Request : ID ";

							$subject .= $hash;

							//$tome = "xxxx@mail.com";

							//$toname = "TV Weekly magazine Support";

							

							//Message sent to TV Weekly

							$mail = new PHPMailer();

							

							$mail->AddReplyTo($fromemail, "".$fromname."");

							$mail->SetFrom($fromemail, "".$fromname."");

							$mail->AddAddress("xxx@mail.com", "TV Weekly magazine Support");

							//$mail->AddAddress($tome, "".$toname."");

							$mail->Subject = "".$subject."";

							//$mail->AltBody = "To view this message, please use an HTML compatible email viewer";

							

							$mail_body .= "<html>";

							$mail_body .= "

							<head>

								<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">

								<title>Contact from TV Weekly magazine</title>

							</head>

							<body>

							<table width=\"625\" border=\"0\" align=\"center\" cellpadding=\"15\" cellspacing=\"0\" style=\"border: 1px solid #ccc;\">

								<tr>

								<td width=\"97\" style=\"border-bottom: 1px dashed #CCC;\"><img src=\"http://www.iwantmytvmagazine.com/images/logo-email.jpg\" alt=\"TV Weekly Magazine Logo\" width=\"95\" height=\"80\" /></td>

								<td width=\"466\" style=\"border-bottom: 1px dashed #CCC;\"><h1 style=\"color: #09F; margin: 0; font: bold 22px Verdana, Geneva, sans-serif;\">TV Weekly Vacation Stop Request</h1></td>

								</tr>

								<tr>

								<td colspan=\"2\">

									<p style=\"font: 12px Arial, Helvetica, sans-serif;\"><strong>Request ID</strong>: ". $hash . "<br />

										<strong>Requested Start Date</strong>: " . $start . "<br />

									<strong>Requested	End Date</strong>: " . $end . "</p>

									<h2 style=\"border-top: 1px dashed #CCC; padding-top: 15px; font: bold 14px Verdana, Geneva, sans-serif; font-size: 16px;\"><strong>Subscriber Information</strong></h2>

									<p style=\"font: 12px Arial, Helvetica, sans-serif;\"><strong>Name</strong>: " . $fromname . "<br />

										<strong>Email Address</strong>: " . $fromemail . "<br />

									<strong>Phone</strong>: " . $phone . "</p>

									<p style=\"font: 12px Arial, Helvetica, sans-serif;\"><strong>Address</strong><br />

										" . $mailing . "<br />

										" . $city . ", " . $state . "<br />

										" . $zip . "

									</p>

									<p style=\"font: 12px Arial, Helvetica, sans-serif;\"><strong>User Information</strong><br />

								User IP: ".$_SERVER[REMOTE_ADDR]."<br />

								Browser info: ".$_SERVER[HTTP_USER_AGENT]."<br />

								Page Sent From: ".$_SERVER[HTTP_REFERER]."</p>

								</td>

							</tr>

							</table>

							</body>

							";	

							$mail_body .= "</html>\n";

							$mail_body = eregi_replace("[\]",'',$mail_body);

							$mail->MsgHTML($mail_body);

							

							//Message sent to person who requested vacation stop

							$mail2 = new PHPMailer();

							

							$mail2->AddReplyTo("xxx@mail.com", "TV Weekly magazine");

							$mail2->SetFrom("xxx@mail.com", "TV Weekly magazine");

							$mail2->AddAddress($fromemail, "".$fromname."");

							//$mail2->AddAddress($tome, "".$toname."");

							$mail2->Subject = "".$subject."";

							//$mail->AltBody = "To view this message, please use an HTML compatible email viewer";

							

							$mail2_body .= "<html>";

							$mail2_body .= "

							<head>

								<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">

								<title>Confirmation from TV Weekly magazine</title>

							</head>

							<body>

							<table width=\"625\" border=\"0\" align=\"center\" cellpadding=\"15\" cellspacing=\"0\" style=\"border: 1px solid #ccc;\">

								<tr>

								<td width=\"97\" style=\"border-bottom: 1px dashed #CCC;\"><img src=\"http://www.iwantmytvmagazine.com/images/logo-email.jpg\" alt=\"TV Weekly Magazine Logo\" width=\"95\" height=\"80\" /></td>

								<td width=\"466\" style=\"border-bottom: 1px dashed #CCC;\"><h1 style=\"color: #09F; margin: 0; font: bold 22px Verdana, Geneva, sans-serif;\">TV Weekly Vacation Stop Request</h1></td>

								</tr>

								<tr>

								<td colspan=\"2\">

									<p style=\"font: 12px Arial, Helvetica, sans-serif;\">Dear  " . $fromname . ",</p>

									<p style=\"font: 12px Arial, Helvetica, sans-serif;\">We have received your vacation stop request and this email is confirmation of the request for your records with information about the request listed below. Please take note of the request id for any future communication with TV Weekly magazine in regards to this request. Thank you.</p>

									<p style=\"font: 12px Arial, Helvetica, sans-serif;\"><strong>Request ID</strong>: ". $hash . "<br />

										<strong>Requested Start Date</strong>: " . $start . "<br />

									<strong>Requested	End Date</strong>: " . $end . "</p>

									<h2 style=\"border-top: 1px dashed #CCC; padding-top: 15px; font: bold 14px Verdana, Geneva, sans-serif; font-size: 16px;\"><strong>Subscriber Information</strong></h2>

									<p style=\"font: 12px Arial, Helvetica, sans-serif;\"><strong>Name</strong>: " . $fromname . "<br />

										<strong>Email Address</strong>: " . $fromemail . "<br />

									<strong>Phone</strong>: " . $phone . "</p>

									<p style=\"font: 12px Arial, Helvetica, sans-serif;\"><strong>Address</strong><br />

										" . $mailing . "<br />

										" . $city . ", " . $state . "<br />

										" . $zip . "

									</p>

									<p style=\"font: 12px Arial, Helvetica, sans-serif;\"><strong>User Information</strong><br />

								User IP: ".$_SERVER[REMOTE_ADDR]."<br />

								Browser info: ".$_SERVER[HTTP_USER_AGENT]."<br />

								Page Sent From: ".$_SERVER[HTTP_REFERER]."</p>

								</td>

							</tr>

							</table>

							</body>

							";	

							$mail2_body .= "</html>\n";

							$mail2_body = eregi_replace("[\]",'',$mail2_body);

							$mail2->MsgHTML($mail2_body);

							

							if (!$mail->Send()) {

							echo '<div class="error" id="error">Mail Error: ' . $mail->ErrorInfo . '</div>';	

							} else {

							echo '<div class="success" id="success">Vacation Stop Request Sent!<br />We have received your request and will send you confirmation of your request.</div>';

Open in new window

0
Comment
Question by:pertrai1
9 Comments
 
LVL 3

Expert Comment

by:dockhand
ID: 33664473
Line 78 looks like it might cause more issues than just messing up the second EMail
0
 

Author Comment

by:pertrai1
ID: 33664936
Could you explain so I understand what the problem is and how to fix? Thank you
0
 
LVL 3

Expert Comment

by:dockhand
ID: 33665148
Actually looking at line 29 you have the same issue.  Syntax Issue: You are missing closing quotes and the Php end of line indicator ;
0
 

Author Comment

by:pertrai1
ID: 33665598
I am not seeing that syntax problem because the closing quotes are on line 62 and 113
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Expert Comment

by:onemadeye
ID: 33667491
How about this ...
by removing codes for sending the 2nd email (at the very bottom of your codes)

if (!$mail->Send()) {

// Error message here...

} else {

// Success message here...

// Place codes to send the 2nd email here

}

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 400 total points
ID: 33667513
I cannot follow the logic here.  Please consider adding some standard control-structure indentation and some comments.

Maybe you have $mail->send but not $mail2->send?
0
 

Author Comment

by:pertrai1
ID: 33668673
Sorry for the bad paste job on my part and not commenting to give you an idea of what I am trying to accomplish. Please see new code and see if this is logical and the comments help you to better understand that we are trying to have the form sent to the company and then when the message has been sent to the company, send a separate confirmation email to the person who submitted the form. I apologize again.

I hope there is a way to make this work. Thank you in advance for any help.

Rob
require_once('../contact/phpmailer/class.phpmailer.php');

							

$fromname = stripslashes(trim($_POST['name']));

$fromemail = stripslashes(trim($_POST['email']));

$phone = stripslashes(trim($_POST['phone']));

$mailing = stripslashes(trim($_POST['mailing']));

$city = stripslashes(trim($_POST['city']));

$state = stripslashes(trim($_POST['state']));

$zip = stripslashes(trim($_POST['zip']));

$start = stripslashes(trim($_POST['start']));

$end = stripslashes(trim($_POST['end']));

$hash = md5(uniqid("up_",true).$_POST['name']);

$subject = "[TV Weekly magazine] - Vacation Stop Request : ID ";

$subject .= $hash;



//Message sent to the company with the requested information

$mail = new PHPMailer();



$mail->AddReplyTo($fromemail, "".$fromname."");

$mail->SetFrom($fromemail, "".$fromname."");

$mail->AddAddress("somebody@mail.com", "Company Name"); // Taken out so nobody knows what company this is but this is going to the company email

$mail->Subject = "".$subject."";

$mail->AltBody = "To view this message, please use an HTML compatible email viewer";



$mail_body .= "<html>";

$mail_body .= "

<head>

	<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">

	<title>Contact from XYZ Website</title>

</head>

<body>

<table width=\"625\" border=\"0\" align=\"center\" cellpadding=\"15\" cellspacing=\"0\" style=\"border: 1px solid #ccc;\">

	<tr>

	<td width=\"97\" style=\"border-bottom: 1px dashed #CCC;\"><img src=\"http://www.websitename.com/images/logo-email.jpg\" alt=\"Logo\" width=\"95\" height=\"80\" /></td>

	<td width=\"466\" style=\"border-bottom: 1px dashed #CCC;\"><h1 style=\"color: #09F; margin: 0; font: bold 22px Verdana, Geneva, sans-serif;\">TV Weekly Vacation Stop Request</h1></td>

	</tr>

	<tr>

	<td colspan=\"2\">

		<p style=\"font: 12px Arial, Helvetica, sans-serif;\"><strong>Request ID</strong>: ". $hash . "<br />

			<strong>Requested Start Date</strong>: " . $start . "<br />

		<strong>Requested	End Date</strong>: " . $end . "</p>

		<h2 style=\"border-top: 1px dashed #CCC; padding-top: 15px; font: bold 14px Verdana, Geneva, sans-serif; font-size: 16px;\"><strong>Subscriber Information</strong></h2>

		<p style=\"font: 12px Arial, Helvetica, sans-serif;\"><strong>Name</strong>: " . $fromname . "<br />

			<strong>Email Address</strong>: " . $fromemail . "<br />

		<strong>Phone</strong>: " . $phone . "</p>

		<p style=\"font: 12px Arial, Helvetica, sans-serif;\"><strong>Address</strong><br />

			" . $mailing . "<br />

			" . $city . ", " . $state . "<br />

			" . $zip . "

		</p>

		<p style=\"font: 12px Arial, Helvetica, sans-serif;\"><strong>User Information</strong><br />

	User IP: ".$_SERVER[REMOTE_ADDR]."<br />

	Browser info: ".$_SERVER[HTTP_USER_AGENT]."<br />

	Page Sent From: ".$_SERVER[HTTP_REFERER]."</p>

	</td>

</tr>

</table>

</body>

";	

$mail_body .= "</html>\n";

$mail_body = eregi_replace("[\]",'',$mail_body);

$mail->MsgHTML($mail_body);



if ($mail->Send()) { // If the message has been sent to the company we will then send a confirmation message to the person who sent the message

$mail2 = new PHPMailer(); // Do I need to instantiate this again?



$mail2->AddReplyTo("info@xyz.com", "XYZ Company"); // This would be the company email and name

$mail2->SetFrom("info@xyz.com", "XYZ Company");

$mail2->AddAddress($fromemail, "".$fromname.""); // Picking up the variables that have stored the name and email from the person filling out the form

$mail2->Subject = "".$subject."";

$mail->AltBody = "To view this message, please use an HTML compatible email viewer";

$mail2_body .= "<html>";

$mail2_body .= "

<head>

	<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">

	<title>Confirmation from XYZ Company</title>

</head>

<body>

<table width=\"625\" border=\"0\" align=\"center\" cellpadding=\"15\" cellspacing=\"0\" style=\"border: 1px solid #ccc;\">

	<tr>

	<td width=\"97\" style=\"border-bottom: 1px dashed #CCC;\"><img src=\"http://www.website.com/images/logo-email.jpg\" alt=\"Logo\" width=\"95\" height=\"80\" /></td>

	<td width=\"466\" style=\"border-bottom: 1px dashed #CCC;\"><h1 style=\"color: #09F; margin: 0; font: bold 22px Verdana, Geneva, sans-serif;\">TV Weekly Vacation Stop Request</h1></td>

	</tr>

	<tr>

	<td colspan=\"2\">

		<p style=\"font: 12px Arial, Helvetica, sans-serif;\">Dear  " . $fromname . ",</p>

		<p style=\"font: 12px Arial, Helvetica, sans-serif;\">We have received your vacation stop request and this email is confirmation of the request for your records with information about the request listed below. Please take note of the request id for any future communication with XYZ company in regards to this request. Thank you.</p>

		<p style=\"font: 12px Arial, Helvetica, sans-serif;\"><strong>Request ID</strong>: ". $hash . "<br />

			<strong>Requested Start Date</strong>: " . $start . "<br />

		<strong>Requested	End Date</strong>: " . $end . "</p>

		<h2 style=\"border-top: 1px dashed #CCC; padding-top: 15px; font: bold 14px Verdana, Geneva, sans-serif; font-size: 16px;\"><strong>Subscriber Information</strong></h2>

		<p style=\"font: 12px Arial, Helvetica, sans-serif;\"><strong>Name</strong>: " . $fromname . "<br />

			<strong>Email Address</strong>: " . $fromemail . "<br />

		<strong>Phone</strong>: " . $phone . "</p>

		<p style=\"font: 12px Arial, Helvetica, sans-serif;\"><strong>Address</strong><br />

			" . $mailing . "<br />

			" . $city . ", " . $state . "<br />

			" . $zip . "

		</p>

		<p style=\"font: 12px Arial, Helvetica, sans-serif;\"><strong>User Information</strong><br />

	User IP: ".$_SERVER[REMOTE_ADDR]."<br />

	Browser info: ".$_SERVER[HTTP_USER_AGENT]."<br />

	Page Sent From: ".$_SERVER[HTTP_REFERER]."</p>

	</td>

</tr>

</table>

</body>

";	

$mail2_body .= "</html>\n";

$mail2_body = eregi_replace("[\]",'',$mail2_body);

$mail2->MsgHTML($mail2_body); // This should send the confirmation email but it is not

}



if (!$mail->Send()) { // If mail has not been sent to company give them this error

echo '<div class="error" id="error">Mail Error: ' . $mail->ErrorInfo . '</div>';	

} else {

echo '<div class="success" id="success">Vacation Stop Request Sent!<br />We have received your request and will send you confirmation of your request.</div>';

}

Open in new window

0
 
LVL 3

Assisted Solution

by:dockhand
dockhand earned 100 total points
ID: 33672579
Ray is correct.  There is no $mail2->send.  Your first one is sent with the "if (!$mail->Send()) { " logic but the second is never sent either implicitly or by checking.
0
 

Author Closing Comment

by:pertrai1
ID: 33678710
Thank you both for your help. My little knowledge of phpmailer did not understand that $mail->send was actually what was used to send the mail. God bless you both for your help in learning about this and for taking the time to help me.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now