Sending mail to both sender and receiver using PHPmailer

风流意气都作罢 提交于 2019-12-13 08:22:20

问题


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);  
}?>

回答1:


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();


来源:https://stackoverflow.com/questions/47647121/sending-mail-to-both-sender-and-receiver-using-phpmailer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!