batch email send with custom content - php

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

Related

Change hyperlink when sending group mails

When sending a group mail about assigning to a newsletter, I need to create a hyperlink button which redirects to: example.com/action.php?id=11 ... email=<%email>
I need to change the <%email> area depending on the recipient's email...
I've done some research, but haven't come up with any solution.
Thanks for any response.
assign variable in code eg
$email = "someone#somewhere.com";
include the variable with the string eg.
echo "example.com/action.php?id=11&email=$email";
You can keep something like <%email> as placeholder text in mail body of each mail text in group mail and later you can replace this with your original email by using str_replace() function when you actually sending mail in your loop before sending mail.
provide complete code you are trying to do so we can help you more with it.

Laravel 5.3 send Mail to multiple email-adresses

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.

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.

Spam check before sending using Zend_Mail

I'm using Zend_Mail and the following code to send my email messages.
$mail = new Zend_Mail('UTF-8');
$mail ->setBodyText($plainBody)
->setBodyHtml($htmlBody)
->setSubject($subject)
->setFrom(FROM_ADDR, FROM_NAME)
->addTo($email, $name )
->addHeader(MY_HEADER, serialize( array( 'foo' => 'bar' ) ) )
;
I need to check the spam rating for the prepared message and I'd like to do it using SpamAssassin.
I thought to create a file with the contents and running something such as exec('spamc $filename'), but how to get the file content with the full MIME body?
I noticed that there's a _buildBody() function in Zend_Mail_Abstract class (library/Zend/Mail/Transport/Abstract.php) that's return that, but that's a protected function.
Thanks
If you want to use SpamAssasin, then run your email message through spamc:
http://spamassassin.apache.org/full/3.1.x/doc/spamc.html
Spamc is the client half of the spamc/spamd pair. It should be used in
place of spamassassin in scripts to process mail. It will read the
mail from STDIN, and spool it to its connection to spamd, then read
the result back and print it to STDOUT. Spamc has extremely low
overhead in loading, so it should be much faster to load than the
whole spamassassin program.
You can do use in PHP by:
Writing the message into a temporary file and running shell_exec('spamc < message.tmp'), or
Running the command with proc_open() then send message via STDIN.
I am assuming you want to simulate a spam check on the recipient's end. That's an interesting idea, but note that the results it will give you will be far from 100% realistic. After all, it's the sending process that adds much of the vital information that helps determine whether an E-Mail is spam (e.g. the sender IP and routes.)
Anyway, to do this, you will probably have to implement a custom Zend_Mail_Transport class, based on the example of Zend_Mail_Transport_Smtp. Any data that transport class sends to the SMTP server, you would have to re-route to a text file. As far as I can see at a cursory glance, it's going to be a bit of work but not impossible.

Categories