使用PHPmailer向发送者和接收者发送邮件 [英] Sending mail to both sender and receiver using PHPmailer

查看:305
本文介绍了使用PHPmailer向发送者和接收者发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在实时服务器中使用phpmailer发送发件人和收件人的电子邮件.它的工作正常,但我想在发件人副本中包含其他消息,例如感谢您向我们注册".我做不到. 你能帮忙吗? 到目前为止我尝试过的代码:

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

推荐答案

我总是创建另一个用于发送另一封电子邮件的邮件对象.因此它将始终选择新消息.

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

第二个选项,使用第二封电子邮件上方的clearAddresses清除您的早期邮件,如下所示:

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

这篇关于使用PHPmailer向发送者和接收者发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆