PHPMailer sending to multiple address - php

I am using PHPMailer to send emails, i use this code to add the email address to send to:
$mail->addAddress("receipent#domain", "receipent");
it works for one email but does not work for multiple email.
i get an error saying
You must provide at least one recipient email address.

Just use it multiple times.
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com');

Just call it one time for one address like
mail->addAddress("receipent1#domain", "receipent1");
mail->addAddress("receipent2#domain", "receipent2");

Related

PHPMailer sending mails through addCC function if mail not exist then skip that email address

I have a scenario in which I have to add multiple addresses in a carbon copy (CC) field from a PHPMailer E-mail. If an E-mail is not valid, currently mail does not get sent.
I want it so that if an E-mail address is not valid, it will skip adding that E-mail address in $mail->addCC($cc);, and simply send to the other recipients.
Are there any alternative ways to achieve this within PHPMailer? If so, please suggest them to me.
You can send emails to cc-recipients as to main recipients like this:
/* Prepare PHPMailer object */
...
/* Send email to main recipient */
...
/* Form cc array */
$ccs= [
"FirstCC" => "first#mail.com",
"SecondCC" => "second#mail.com"
];
/* Send email to each cc as to main */
foreach ($ccs as $name => $address)
{
$mail->clearAddresses();
$mail->AddAddress($address, $name);
$mail->Send();
}
This way will provide assurance that all valid recipients get email
PHPMailer automatically validates all addresses you give it. The only way you can tell if an address is actually going to be accepted is to try to send to it, and if it fails, don't try to send to it next time. If you want to send to multiple CCs (where all recipients can see all other addresses in the message), do this:
...
$mail->addAddress('me#example.com');
foreach ($ccs as $cc) {
$mail->addCC($cc);
}
$mail->send();
addCC will automatically skip actually invalid addresses (returning false for any that are invalid), but it's not possible to establish whether addresses will be accepted for delivery at this point - you have to attempt to deliver to find out.

how to php email change `setfrom`

how to change setfrom in email sending by php
example I have the sender email is myemail1#gmail.com and I want change the setfrom when email is sending to myemail2#gmail.com
I tried with this code but not work
$mail->setFrom('myemail2#gmail.com', 'My Email2');

phpmailer change mail sender

When I send mail from phpmailer and I wanted to response I get response e-mail address like admin#domain.com. But I want change it to office#domain.com.
So I added:
$mail->AddReplyTo('office#domain.com', 'First Last');
But in e-mails to response I get both (office and admin) and I want only office#domain.com
I changed it to:
$mail->Sender='admin#domain.pl';
$mail->SetFrom('office#domain.pl','First Last');
I get
SMTP Error: Data not accepted.
SMTP server error: 5.7.1 Forged sender address:
My phpmailer version is: 5.2.6
The reply to addresses needed to be added before the from address:
$mail->addReplyTo('replyto#email.com', 'Reply to name');
$mail->setFrom('mailbox#email.com', 'Mailbox name');
With this order all is OK.
addReplyTo not AddReplyTo
Alternative:
You can clear replyTo array before:
$mail->ClearReplyTos();
$mail->addReplyTo(example#example.com, 'EXAMPLE');
Setting Sender is the correct approach to do this, so you're doing that right. The error you are seeing is probably down to SPF checks at the receiver - if the sender domain has SPF set up and it does not allow sending from your IP, it will reject it with the error you are seeing.

PHPMailer and multiples destination addresses issue

Im using phpmailer to send emails and i check that when i add some addresses and just one of them is an invalid address (not exists) E.G. "asdfasfasf#asdfasdfsfsfs.commm" and send the email i see that the email was sent (to correct addresses) and i have no idea how to check if one of the adresses is wrong to be able to log that issue before sending the email.
The code to send and add addresses is this:
foreach($options['emails'] as $email){
$mmail->AddAddress($email[0], $email[1]);
}
if (!$mmail->Send()) {
echo "error";
}else {
echo "sent";
}
Thanks in advance
Take a look at filter_var to validate the syntax:
if (filter_var($email[0], FILTER_VALIDATE_EMAIL)) {
// email address is considered valid
Note that there are ways to connect to the recipients SMTP server and ask if the email actually exists (see https://code.google.com/p/php-smtp-email-validation/ for example) however many email servers won't honor these queries anymore, due to spammer abuse.

Cannot send mail to yahoo (any address) using PHP code and SMTP server

I have the following code which sets up the SMTP server:
ini_set("send_from", "test#gmail.com");
ini_set("SMTP", "smtp.gmail.com");
and i create a simple mail in this way:
mail("test#yahoo.com", "A subject", "My message for you", "From: TEST");
When I run this code, it fails to send mail to Yahoo e.g. some.email#yahoo.com. But when i use any Gmail mail address as the first argument, it works.
What's wrong ?
To send mail as an authenticated user you should use email authentication methods like SPF, DKIM etc.
Also you need to make sure your domain should point to your IP address and IP address MUST point to same domain. This is called Reverse DNS
Other good practice that prevents mails from going into spam folder are
Make sure you have a unsubscribe link
Make sure the Reply-To header is added and the email used here is a valid email.
Add a Name in the To field. Like First Last <email#example.com>
Add a postal address of the company you are mailing from which must include a phone number.
There was a form to white list email senders IP for yahoo. Now I dont find it. So try the above things, It should work well.
In thi case you dont auth (user name passwort) and dont usw tls. This wont be accepted.
Better use this:
XAMPP Sendmail using Gmail account
or an framework to send emails via smtp like
Zend Mail Gmail SMTP
http://framework.zend.com/manual/1.12/en/zend.mail.sending.html
Here a code example
http://framework.zend.com/downloads/latest#ZF1
require('Zend/Mail.php');
$config = array(
'ssl' => 'tls',
'port' => 587,
'auth' => 'login',
'username' => 'your_gmail_address#gmail.com',
'password' => 'password'
);
$smtpConnection = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
Zend_Mail::setDefaultTransport($smtpConnection);
Zend_Mail::setDefaultFrom('your_gmail_address#gmail.com', 'Your real name');
$mail = new Zend_Mail();
$mail->addTo('any_address#yahoo.com', 'Test');
$mail->setSubject(
'Demonstration - Sending Mails per SMTP Connection'
);
$mail->setBodyText('...Your message here...');
$mail->send($smtpConnection);

Categories