Asked  7 Months ago    Answers:  5   Viewed   30 times

I am newbie with PHPMailer library , I want to get emails from my contact form But on submission of form I want sender also get a copy of mail With additional message as " thanks for registering with us." As I gone through PHP mailer examples We can use addCC();. But how to embed additional msg.

Could you please help me with this ?

 Answers

85

Using CC or BCC will always result in identical message being sent to all; If you want the messages to be different for different recipients, you must send separate messages with different bodies. With PHPMailer:

$mail->addAddress('recipient1@example.com');
$mail->Body = "hello abc";
$mail->send();
$mail->clearAddresses();
$mail->addAddress('recipient2@example.com');
$mail->Body = "hello xyz";
$mail->send();

It's important to call clearAddresses as otherwise the second message will be sent to both recpients.

Friday, May 28, 2021
 
Sufi
answered 7 Months ago
50

There are two problems here:

  • You are not removing the address from the mail, so when you add a second one, the first one is still there and both will see the other address.
  • You are using a header redirect in your loop without terminating your script. This can cause code after the redirect to run, but there is no guarantee for how much and how long.

The first problem you can solve by clearing the recipients at the end of the loop:

$mail->ClearAllRecipients();

As for the second problem, you should not redirect anywhere inside the loop and when you redirect after all messages have been sent, you should exit your scipt using exit; so that nothing gets executed after that.

Saturday, May 29, 2021
 
Classified
answered 7 Months ago
41

After trying various ways, i found following code working with almost all email providers

$to['email'] = "recipients email address";      
$to['name'] = "name";   
$subject = "email subject";
$str = "<p>Hello, World</p>";
$mail = new PHPMailer;
$mail->IsSMTP();                                     
$mail->SMTPAuth = true;
$mail->Host = 'Specify main and backup server here';
$mail->Port = 465;
$mail->Username = 'xyz@domainname.com';
$mail->Password = 'email account password';
$mail->SMTPSecure = 'ssl';
$mail->From = 'From Email Address';
$mail->FromName = "Any Name";
$mail->AddReplyTo('xyz@domainname.com', 'any name'); 
$mail->AddAddress($to['email'],$to['name']);
$mail->Priority = 1;
$mail->AddCustomHeader("X-MSMail-Priority: High");
$mail->WordWrap = 50;    
$mail->IsHTML(true);  
$mail->Subject = $subject;
$mail->Body    = $str;
if(!$mail->Send()) {
$err = 'Message could not be sent.';
$err .= 'Mailer Error: ' . $mail->ErrorInfo;                        
}

$mail->ClearAddresses();

variable values needs to be changed accordingly. Hope these helps people having issues with PHPmailer

Saturday, May 29, 2021
 
Neysor
answered 7 Months ago
73

If this code is running, you should be seeing a ton of debug output, even if it is working correctly. You don't actually say what the problem is, but you're doing a few things wrong that I can see. It would really help if you based your code on the examples provided and read the docs instead of just guessing.

$mail->SMTPSecure = 'TLS';

should be:

$mail->SMTPSecure = 'tls';

Don't call smtpConnect() yourself, you'll mess up the tracking of SMTP transaction state. If you want to set SSL params, set them the expected way and then just call send(), which will deal with the connection:

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

The next question is why are you doing that? If you can't provide an explicit, specific reason for doing that, you're doing something wrong.

Saturday, May 29, 2021
 
waylaidwanderer
answered 7 Months ago
67

If your destination phone number is in India, your Sender ID must be registered to be able to work. You can request to register a Sender ID by opening a support ticket with AWS support (which has no cost).

Other countries require no registration, thus, some deliveries will work by just adding a sender ID value to your publish request.

You might successfully get a working sender ID to an Indian destination depending on the route the message takes to reach an end user. However, the right course of action (given technology partners and provider regulations) is to register the sender ID before using it with AWS (which is why it's not marked on the website).

Disclosure: I work at the AWS team for Mobile Messaging.

Sunday, October 17, 2021
 
mgierw
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share