Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Wrapping Up The Project Using A Third-Party Library

How to send two different email with different bodies using phpmailer ?

I have a form on which user submits some information along with his email address. I want that a mail comes to me with his details and a thank you mail goes to him on his email. Please Help...!!!

2 Answers

Gareth Borcherds
Gareth Borcherds
9,372 Points

All you need to do is call the send_mail function twice. Something like:

require 'PHPMailerAutoload.php';

$mail = new PHPMailer();
/*Set Mail settings for first email */


$mail = new PHPMailer(); //We reset the $mail variable to a new instance of the class
/*Set Mail settings for second email */


By doing this you essentially reset the class and can send the second email. You can also declare a second instance of the class by changing the variable in the second instance.

Kevin Korte
Kevin Korte
28,148 Points

It my not be the best way to do it, but I dont see why you couldn't initialize two PHPmailer objects with a form submit. The first PHPmailer object could email you with the message details, the second mailer object shoots an email to whoever submitted the form. I didn't see anything in the docs that allowed this to happen within just one mailer object, but I only took a few minutes to glance over it all. But...

From a pure user experience point of view, if I submit your form, clouding up my email box with a email that only says something like "Thank you, your message was received, well be in touch shortly" is poor user experience. Don't take up my time by making me organize or delete this random email in my box. If that is the case, a simple confirmation on screen is more than enough for me to know the message went through.

The only time I think it's okay to send an email after a contact form submit is when the email contains information just for me; such as maybe a reference or ticket number when I'm inquiring about technical help; or emailing me a receipt if the form is a purchase. If you're going to send me an email, make sure it contains information somewhat private or personal.

Just ask yourself what benefit is the form submitter getting from your email. Thats all :)

Tim Stride
Tim Stride
13,036 Points

I just found this old thread after searching with a similar enquiry. I agree that you have to ask yourself what the benefit is, but with an enquiry contact form, for example, it might be useful to send a confirmation email to the user that includes a copy of their enquiry for their own reference. Otherwise, once they navigate away from the contact form section, they have no record of what they sent in the same way that they would if they had sent an email directly from their email account.

I find that useful if I submit an enquiry through a website, anyway!