-1

I am using phpmailer in live server for sending emails for both sender and receiver. Its working fine but I want to include additional message in sender copy as "thanks for registering with us". I am unable to do it. Could you please help with this ? Code I tried So far:

<?php
$msg = "";

if (isset($_POST['submit'])) {

    require 'phpmailer/PHPMailerAutoload.php';

    function sendemail($to, $from, $fromName, $body) {
        $mail = new PHPMailer();
        $mail->setFrom($from, $fromName);
        $mail->addAddress($to);

        $mail->Subject = 'Contact Form - Email';
        $mail->Body = $body;
        $mail->isHTML(false);
        return $mail->send();
    }
    function sender_mail($to, $from, $fromName, $body) {
        $mail = new PHPMailer();
        $mail->setFrom($from, $fromName);
        $mail->addAddress($to);

        $mail->Subject = 'Copy Contact Form - Email';
        $mail->Body = $body . 'Thanks for registering with us';
        $mail->isHTML(false);

        return $mail->send();
    }
    $name = $_POST['username'];
    $email = $_POST['email'];
    $body = $_POST['body'];
   sendemail('abc@domain.com', 'xyz@domain.com', $name, $body);
    sender_mail('abc@domain.com', $email, $name, $body);  
}?>
Sowmya S
  • 65
  • 1
  • 10

1 Answers1

0

I always create different mail object for sending another email. So it will always pick new message.

For sending another email you can update like below:

    $mail2 = new PHPMailer();
    $mail2->setFrom($from, $fromName);
    $mail2->addAddress($to);

    $mail2->Subject = 'Copy Contact Form - Email';
    $mail2->Body = $body . 'Thanks for registering with us';
    $mail2->isHTML(false);

    return $mail2->send();

2nd option to use clearAddresses above your second email to clear your earlier messages like below:

    $mail->clearAddresses();

    $mail = new PHPMailer();
    $mail->setFrom($from, $fromName);
    $mail->addAddress($to);

    $mail->Subject = 'Copy Contact Form - Email';
    $mail->Body = $body . 'Thanks for registering with us';
    $mail->isHTML(false);

    return $mail->send();
Amit Gupta
  • 2,771
  • 2
  • 17
  • 31
  • @ Amit Gupta Thanks a lot Problem has been resolved – Sowmya S Dec 05 '17 at 07:20
  • That's a waste of resources. See [this answer](https://stackoverflow.com/a/47632040/333340) for a more efficient approach. – Synchro Dec 05 '17 at 07:23
  • @Synchro Yes you are right but what if the body and subject is different for both emails. Can we repeat $mail->Body for 2 emails with different message in same Page? – Amit Gupta Dec 05 '17 at 07:26
  • Yes - my example shows using a different `Body` for each message, but you can equally change the `Subject` too. – Synchro Dec 05 '17 at 07:31
  • Ok I got it. You have added $mail->clearAddresses(); to clear the earlier messages. Great job. Thanks for your advice. – Amit Gupta Dec 05 '17 at 07:35
  • It's important to call clearAddresses as otherwise the second message will be sent to both recpients. Right Sir. – Amit Gupta Dec 05 '17 at 07:36
  • @SowmyaS Please accept my answer if your issue has been resolved. You will also get 2 points. Happy Coding :) – Amit Gupta Dec 07 '17 at 08:16