Question

I want to send mail using sendmail to multiple recipients in single call. I am using cakephp 1.3. Sending mail to single recipient works fine. I was wondering if I could send mail to multiple recipients(typically around 2-5 recipients).

$this->Email->smtpOptions = array('port'=>'587', 'timeout'=>'30', 'host' => 'smtp.sendgrid.net', 'username'=>'****', 'password'=>'****', 'client' => '***');
$this->Email->delivery = 'smtp';
$this->Email->from = 'Swiso Support <support@swiso.net>';
$this->Email->to = $user['email'];
$this->Email->subject = $title;
$this->Email->template = 'email';
$this->Email->sendAs = 'both';
$this->Email->send();

Could I pass an array of recipients to $this->Email->to .

I appreciate any help.

Was it helpful?

Solution

Googling "cakephp email" reveals this:

CakePHP 1.3 cookbook: Email

it should give you what you need: There is, for example, the bcc field that allows you to send mails to multiple recipients.

The book also has a chapter on sending multiple messages in a loop.

OTHER TIPS

Short answer: No. I have been looking into this too (using Sendgrid), and unless you want to use the bcc field, there isn't a way to do it in the same call. You shouldn't worry about just sending them in a loop though.

foreach($recipients as $recipient) {
    $this->Email->to .= $recipient['email'] . ",";
}

Sending a BCC is a better way of doing this, if you don't want the recipients being able to see to whom you sent it to. In that case it would be

$this->Email->bcc

instead of

$this->Email->to

http://book.cakephp.org/1.3/en/view/1284/Class-Attributes-and-Variables

to: Address the message is going to (string). Separate the addresses with a comma if you want to send the email to more than one recipient.

cc: array of addresses to cc the message to.

bcc: array of addresses to bcc (blind carbon copy) the message to.

Examples:

$recipients = array("email1@gmail.com", "email2@yahoo.com");
$this->Email->to = implode(",", $recipients);
$this->Email->cc = $recipient;
$this->Email->bcc = $recipient;

I dont know much about CakePHP but Sendgrid provides an easy way for sending single mail to multiple recipients in php and you can convert this code to Cakephp,

Sendgrid provides Mail::addTos method in which we can add multiple mails to whom we want to send our mail,

we have to pass associative array of user emails and user names into addTos.

see below example:

$tos = [ 
        //user emails       =>  user names  
        "user1@example.com" => "Example User1",
        "user2@example.com" => "Example User2",
        "user3@example.com" => "Example User3"
    ];
    $email->addTos($tos);

If you want to see Full example which is provided in sendgrid-php github library then i have included it below, so you can understand the whole example:

<?php
require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// require("<PATH TO>/sendgrid-php.php");
// If not using Composer, uncomment the above line and
// download sendgrid-php.zip from the latest release here,
// replacing <PATH TO> with the path to the sendgrid-php.php file,
// which is included in the download:
// https://github.com/sendgrid/sendgrid-php/releases

$email = new \SendGrid\Mail\Mail(); 
$email->setFrom("test@example.com", "Example User");
$tos = [ 
    "test+test1@example.com" => "Example User1",
    "test+test2@example.com" => "Example User2",
    "test+test3@example.com" => "Example User3"
];
$email->addTos($tos);
$email->setSubject("Sending with Twilio SendGrid is Fun");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
    "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
    $response = $sendgrid->send($email);
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '.  $e->getMessage(). "\n";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top