Laravel 5.3 send Mail to multiple email-adresses - php

I'm using Laravel 5.3 and trying to send a newsletter Mail to specific mail adresses.
Currently I do the following:
$newsletterMailAdresses = Newsletter::where('user_id', $userID)->pluck('mailAdress');
This returns an array of email-adresses, to which the emails shouuld be sent.
What I then (normally) use to send mails is:
Mail::to("someMailAdress")->send(new newsletterMail($newsletterText));
I thought about just passing that array into the to function but I'm quite sure it won't work.... What you propably could do, is a foreach with every mail adress and send the mails, but is this the way how this should be done or is there a better way?
PS: I know that thread but its about Laravel 4, so there are many things changed.

I think it should work, when you pass an array. Check the reference:
https://laravel.com/api/5.3/Illuminate/Mail/Message.html#method_to
The first parameter can be an array. Try it.

Mail::to(['array','of','emails'])...
also
Mail::cc(['array','of','emails'])...
and
Mail::bcc(['array','of','emails'])...

I think it should work, but you can also try to do like this:
Mail::raw('No Body', function($message) use ($emails)
{
foreach ($emails as $email)
{
$message->to($email);
}
});
I use like this in my application and it works fine!
Maybe you can also try to send one email and vereify it works fine, then you can try sending the email using the array of emails.

From the Laravel 5.4 documentation on Sending Mail:
The to method accepts an email address, a user instance, or a collection of users. If you pass an object or collection of objects, the mailer will automatically use their email and name properties when setting the email recipients, so make sure these attributes are available on your objects.

Related

Is there a way to get the body of a Laravel Mail object to store in a variable?

I'm using the Laravel Mail API to generate mailables and send them out.
Everything is working fine with that.
However, I also want to log the email bodies in the DB.
In order to do that though, I need to first get the bodies of the emails I'm sending out.
In the Laravel docs, they have a snippet about how to output an email to the browser here:
https://laravel.com/docs/5.7/mail#previewing-mailables-in-the-browser
However, I don't want to output the email to the browser. I want to store it in a variable.
Is that possible? If so, how? Thanks.
Like the docs suggested, I tried something like the following:
$body = new App\Mail\InvoicePaid($invoice);
However, I got an error saying that I have to actually return the new Mail object in order for it to work (i.e., do what they do in the example code in the docs).
Previous paragraph tells you how to capture the rendered template:
$html = (new App\Mail\InvoicePaid($invoice))->render();
echo $html;

unit testing Mail::send()

Say I'm sending an email with \Illuminate\Support\Facades\Mail::send(). How would I test this to see if an email had in fact been sent?
https://laravel.com/docs/5.8/mocking#mail-fake mentions Mail::assertSent() but that requires you a pass to it a #param string $mailable variable and idk what that variable would need to be with Mail::send().
Any ideas?
It looks like you need to call Mail::fake() while testing, and Mail::send() when in production. That should be part of your testing facade.
Even if you your code claims the mail was sent, you'll may want to go a step further and verify the mail was actually received. This will require access to the destination mail server (hard!) ... or you could use something more public like Dispostable.

Use PHPmailer or regualer PHP mail to send text: From $Phone-Number

Can I send a text message using PHP or PHPmailer and in the return use a phone number and not an email?
PHP mail() and PHPmailer makes me use a valid email with an "#" symbol. Is there a way around this?
You can put whatever you want in the from field, but don't expect your mail server, or the receiving mail server to accept it.
Better to set your from: field to something like this instead:
"123-123-1234 <noreply#yourdomain.com>"
That way, you can have a valid from address, and still display a number to the end user.

block sending emails that are not from a specific domain

I would like to filter emails sent. Emails are sent with the PHP mail() function. I would like, without modifying any PHP file if possible, to let emails out only emails that are to a specific domain, and not others. I don't have access to the SMTP server.
Just in case this helps someone ... If the emails are sent after a form is submitted (or similar action), you could change the action attribute of the form html element to point to a new php file that acts as a filter. Once passed (if so) you redirect to the "proper" destination to send the emails. The filtering could be something as easy as:
$good = "*#mydomain.foo, *#localhost";
$good = explode(',', $good);
if (pattern_grep($_POST['email'], $good)) {
// action
}
You should be able to look at the associative array for the "to" field and use the php regex class to match domains that you blacklist.

batch email send with custom content

I want to send out batch mails using SwiftMailer but just wondering what the best option for this would be. The problem is the email content needs to be customized, i.e. there will be a salutation at the top, and a custom link.
Here is my current OOP code:
foreach($suppliers as $supplier)
{
$quote=new Quote();
$quote->enquiry_id=$enquiry->id;
$quote->supplier_id=$supplier->id;
if($quote->save())
{
$supplier_emails[]=$supplier->email;
}
}
$message=new SwiftMailMessage;
$message->setTo($supplier_emails);
$message->setFrom($params['adminEmailFromAddress'] => $params['adminEmailFromName']);
$message->setBody('Here is the message itself')
App::app()->mail->batchSend($message);
I am using a container for SwiftMailer. So as you can see, I can easily specify an array of email address to send the message to. How can I now customize the content? The variables I need to include in the content are $supplier->name and $supplier->link.
Personally I can't see how this can be done, other than sending each email individually in the foreach() loop. IF that is the case, then is it not better to just use the internal PHP mail() function?
You could try using the Decorator plugin:
http://swiftmailer.org/docs/decorator-plugin

Categories