Creating a campaign
On this page:
To send an email to all subscribers of your list, you must create a campaign.
A campaign can be created like this:
Campaign::create() ->from('sender@example.com') ->subject('My newsletter #1') ->content($html) ->to($emailList);
Alternatively, you could manually set the attributes on a Campaign
model.
Campaign::create([ 'from_email' => 'sender@example.com', 'subject' => 'My newsletter #1', 'html' => $html, 'email_list_id' => $emailList->id, ]);
Setting the content and using placeholders
You can set the content of a campaign by setting it’s HTML
attribute.
$campaign->html = $yourHtml; $campaign->save();
In that HTML you can use these placeholders which will be replaced when sending out the campaign:
-
{{ unsubscribeUrl }}
: this string will be replaced with the URL that, when hit, will immediately unsubscribe the person that clicked it -
{{ unsubscribeTag['your tag'] }}
: this string will be replaced with the URL that, when hit, will remove the “your tag” tag from the subscriber that clicked it -
{{ webviewUrl }}
: this string will be replaced with the URL that will display the content of your campaign.
If there is no way for a subscriber to unsubscribe, it will result in a lot of frustration on the part of the subscriber. We always recommend using {{ unsubscribeUrl }}
in the HTML of each campaign you send.
Setting a from name
To set a from name, just pass the name as a second parameter to from
Campaign::create()->from('sender@example.com', 'Sender name')
Setting a reply to
Optionally, you can set a reply to email and name like this
Campaign::create()->replyTo('john@example.com', 'John Doe')