Jump to content

Send multiple emails from on process with phpMailer


louisstephens
 Share

Recommended Posts

Ok, so I have the following phpMailer script that works perfectly. However, I also need this to send a completely different email to the person who filled out the form with basic instructions. I am very new to phpmailer (much quicker and easier to work with). 

Just to lay it all out what I am trying to accomplish:

On submit, the php submits the form data to myself via email, but at the same time it sends another email to the user who completed the form with instructions. Has anyone accomplished anything like this before?

 

        <?php
    require("class.phpmailer.php");
    require_once('class.smtp.php');
    
    $mail = new PHPMailer();
    
    $mail->IsSMTP();  // telling the class to use SMTP
    $mail->SMTPAuth = true;
    $mail->Host = "********.****";
    $mail->Port = *****;
    $mail->IsHTML(true);
    
    $mail->Username = "******";
    $mail->Password = "**********";
    
    
    $mail->From     = "*******@********.com";
    $mail->AddAddress("*****@****.***");
    
    $field1= Trim(stripslashes($_POST['firstName']));
    $field2= Trim(stripslashes($_POST['lastName']));
    $field3= Trim(stripslashes($_POST['emailAddress']));
    $field4= Trim(stripslashes($_POST['phoneNumber']));
    
    // Email body text
    
    
    
    
    
    
    $mail->Subject  = "First PHPMailer Message";
    $mail->Body     .= "First Name: $field1";
    $mail->Body     .= "Last Name: $field2";
    $mail->Body     .= "Email Address: $field3";
    $mail->Body     .= "Phone Number: $field4";
  
    if(!$mail->Send()) {
      echo 'Message was not sent.';
      echo 'Mailer error: ' . $mail->ErrorInfo . $mail->SMTPDebug = 2;
    } else {
      echo 'Message has been sent.';
    }
       
     ?>

 

Link to comment
Share on other sites

What about making a new  PHPMailer object in the success statement block and send a new email with the informations the user has submited ?

Example:

[...]

else {
    echo 'Message has been sent.';

    // create a new PHPMailer object
    $mail = new PHPMailer();
    $mail->From     = "email@domain.com";
    $mail->FromName = "Contact";
    // add the address user
    $mail->AddAddress($field3);
   
    $mail->Subject  = "Thanks for contacting us";
    $mail->Body     = "Thanks for contacting us at our Site."

    $mail->send();
}

 

For information, there is a new PHPMailer module available and also WireMailSmtp.

Link to comment
Share on other sites

I'm not sure you even need a new object - just try setting a new subject, body and recipient after you've sent the first one and send again should work in most mail libraries I've used with PHP.

Link to comment
Share on other sites

Thanks everyone! I thought I had it figured out, but alas I have encountered an issue (a very odd one at that). I added in another addAddress

      $mail->AddAddress($field3, "Bob");

However, mailPhp is sending the email to the original recipient, and the new email is also going to the previous recipient:

        <?php
    require("class.phpmailer.php");
    require_once('class.smtp.php');
    
    $mail = new PHPMailer();
    
    $mail->IsSMTP();  // telling the class to use SMTP
    $mail->SMTPAuth = true;
    $mail->Host = "********.****";
    $mail->Port = *****;
    $mail->IsHTML(true);
    
    $mail->Username = "******";
    $mail->Password = "**********";
    
    
    $mail->From     = "*******@********.com";
    $mail->AddAddress("*****@****.***");
    
    $field1= Trim(stripslashes($_POST['firstName']));
    $field2= Trim(stripslashes($_POST['lastName']));
    $field3= Trim(stripslashes($_POST['emailAddress']));
    $field4= Trim(stripslashes($_POST['phoneNumber']));
    
    // Email body text
    
    
    
    
    
    
    $mail->Subject  = "First PHPMailer Message";
    $mail->Body     .= "First Name: $field1";
    $mail->Body     .= "Last Name: $field2";
    $mail->Body     .= "Email Address: $field3";
    $mail->Body     .= "Phone Number: $field4";
  
    if(!$mail->Send()) {
      echo 'Message was not sent.';
      echo 'Mailer error: ' . $mail->ErrorInfo . $mail->SMTPDebug = 2;
         $mail->AddAddress($field3, "Bob");

      $mail->Subject  = "Thanks for contacting us";
      $mail->Body     .= "Thanks for contacting us at our Site.";


      $mail->Body     .= "<html>
      <head></head>
      <body>
      <table width='550' cellpadding='0' cellspacing='0' style='font-size:10px;font-family:Helvetica;'>
      <tr>
        <td><p>Lorem Ipsum</p<</td>
      </tr>
      </table>
      </body>
      </html>";

      $mail->send();
      echo 'Message has been sent.';

    }
       
     ?>

Do I possibly have something out of place? Just by looking at it, I would assume phpMailer would email the first email to the email address at the top, and the second email would be sent to $field3.

Link to comment
Share on other sites

You should use clearAddresses() before sending the second email :

[...]

if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!<br>Sending an email to the submitted email from user...<br>";
    // clear addresses
    $mail->clearAddresses();
    //Set who the message is to be sent to
    $mail->addAddress($field3, 'Bob');
    $mail->Subject = 'Thanks for contacting us | sent once';
    $mail->Body .= "Thanks for contacting us at our Site.";
    // send email
    $mail->send();
}

This should do the work.

  • Like 1
Link to comment
Share on other sites

You might also want to consider making use of PW's sanitizer, rather than simply using stripslashes.

Maybe not worth changing at this point, but I would also recommend SwiftMailer over PHP Mailer. I don't honestly remember the reasons - it's been a lot of years since I first chose Swift, but it certainly seemed like the better choice at the time. Seems like they are both very actively developed now, so maybe it makes no difference.

Actually, I think maybe the reason at the time was to do with being able to send multipart text and html versions together with proper mimetypes. Maybe PHP Mailer does that now too, or maybe it always did, but swift seemed so much easier.

Link to comment
Share on other sites

15 minutes ago, adrian said:

You might also want to consider making use of PW's sanitizer, rather than simply using stripslashes.

Maybe not worth changing at this point, but I would also recommend SwiftMailer over PHP Mailer. I don't honestly remember the reasons - it's been a lot of years since I first chose Swift, but it certainly seemed like the better choice at the time. Seems like they are both very actively developed now, so maybe it makes no difference.

Actually, I think maybe the reason at the time was to do with being able to send multipart text and html versions together with proper mimetypes. Maybe PHP Mailer does that now too, or maybe it always did, but swift seemed so much easier.

Thanks adrian. I will definitely check out swiftmailer as well. I am not completely sold on PHP Mailer (as I am still learning and getting comfortable with it), but it was the only thing that I could remember at the time that made attachments a breeze.

Link to comment
Share on other sites

 Share

×
×
  • Create New...