Transactional Email using Mailgun Email Template and Dynamic content Apart - php

I have successfully setup a Mailgun account for Transaction email.
The Problem;
I want my email content which are dynamic inside my Email Template please help and even my file attachment not working
<?php
$filePath='#/home/allinclende/public_html/apply/CIW.pdf';
$message = $message;
$html = file_get_contents('email_1.html');
$html .= $message;
$mgClient = new Mailgun('key-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
//enter domain which you find in Default Password
$domain = "apply.allinc.com";
# Make the call to the client.
$result = $mgClient->sendMessage($domain, array(
'from' => 'Jane Doe <jdoe#allinc.com>',
'to' => $add,
'cc' => 'inquiry <info#allinc.com>',
'subject' => $subject,
'text' => $message,
'html' => $html,
'attachment[1]' => $filePath
));
?>

In your email_1.html file include some unique placeholders. Make an array containing them:
$search = array("FIRSTNAME", "LASTNAME", "APPOINTMENTDATE");
Query your database and get the variables to replace each one, make an array of those:
$replace = array($firstname, $lastname, $appointmentdate);
Then it's as simple as:
$customhtml = str_replace($search, $replace, $html);
Send $customhtml via mailgun, then loop through again to personalise the email for each customer
Correct construct for sending an attachment is like this:
$result = $mgClient->sendMessage($domain, array(
'from' => 'Jane Doe <jdoe#allinc.com>',
'to' => $add,
'cc' => 'inquiry <info#allinc.com>',
'subject' => $subject,
'text' => $message,
'html' => $html
), array(
'attachment' => array('#/home/allinclende/public_html/apply/CIW.pdf')
));

Related

How to add attachment to mailgun API using PHP

I have been using the mailgun API to send emails in my PHP application, When I add the attachment parameter as per the documentation, I got an error:Invalid resource type: array in /var/www/html/vendor/guzzlehttp/psr7/src/functions.php
Can someone assist?
$mgClient = new Mailgun('xxx');
$domain = "xxx";
$parameters = array(
'from' => 'xxx',
'to' => $to,
'subject' => $subject,
'text' => $text,
'attachment' = [
[
'filePath' => $attachment,
'filename' => $file_name,
]
];
);
$result = #$mgClient->sendMessage("$domain", $parameters);
I found a solution by adding the attachment array as a third parameter of the sendMessage()
$mgClient->sendMessage("$domain", $parameters, ['attachment' => ['filePath' => $file_path]]);

Pear PHP Mail sending CC doesn't seem to work

I'm using PEAR's mail library via SMTP on my server and whilst I can get emails to generate, adding CC's doesn't seem to work. Basically the CC recipients never get their email even though the main recipient of the same email does.
My basic setup is as follows, all the recipient variables ($to,$cc,$bcc) are string variables containing either a single recipient email addresses or comma separated email addresses.
$headers = array (
'From' => $from,
'To' => $to,
'Cc' => $cc,
'Bcc' => $bcc,
'Subject' => $subject,
'Reply-To' => $from,
'X-Mailer' => 'PHP/' . phpversion(),
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=ISO-8859-1'
);
$smtp = Mail::factory('smtp', array (
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password
));
$result = $smtp->send($to, $headers, $message);
I've read that sending BCCs is more complex, so lets stick to the CCs... why aren't they being received ? Is there anything obvious that I'm doing wrong here ?
In order to send an email to CC or BCC with SMTP, you must list all email addresses both under recipients to the send() function and in the CC key within the header.
$to = "john#example.com";
$cc = "doe#example.com";
$recipients = $to . ", " . $cc;
$headers["From"] = "john#example.com";
$headers["To"] = $to;
$headers["Subject"] = "Hello World!";
$headers["Cc"] = "doe#example.com";
$headers["Reply-To"] = "john#example.com";
$send = $mail->send($recipients, $headers, $body);

How to send logo in the body of email in php?

I am attaching my email code. I want to add an image in my body section with a message in this code.
require_once "../lib/Mail-1.4.1/Mail.php";
$from = 'someone#gmail.com';
$to = $mail;
$subject = 'Account Confirmation';
$body = 'Thanks for registering yourself, your login credentials are as
follows;'."\r\n".'Your User Name is: '.$uname.''."\r\n".'Your Password is:
'.$pass1.'';
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'someone#gmail.com',
'password' => '12345678'
));
$mail = $smtp -> send($to, $headers, $body);
if(PEAR::isError($mail)){
echo '<p>'.$mail->getMessage().'</p>';
}
else{
echo "<script>alert('Successfully Registered! Your credentials have been
sent on email that you have given in the form')</script>";
}
this is my email code, in body I want to add an image/logo.
Use for embedding image AddEmbeddedImage
you can also add an image as inline attachment with the content ID of my-photo, you would access it within the HTML body using <img src="cid:my-photo" alt="my-photo">.
In detail, here is the function to add an embedded attachment:
$mail->addEmbeddedImage($filename, $cid);

How to create a dynamic array for Mandrill API

I want to send an email to multiple email addresses using one Mandrill API call, here is what we do to send to only one email:
<?php
$mandrill_message = array(
// ...blah blah, doesn't matter
// To send to only one email address:
'to' => array(
array(
'email' => $the_email,
)
),
// ...blah blah, doesn't matter
);
?>
This is OK but while we want to send the message to multiple email addresses, we must have this:
<?php
$mandrill_message = array(
// ...blah blah, doesn't matter
// To send to more than one email:
'to' => array(
array(
'email' => $email_1,
),
array(
'email' => $email_2,
),
array(
'email' => $email_3,
),
),
// ...blah blah, doesn't matter
);
?>
As you can see, we've repeated the array part, now assuming we have this array:
$subscribers_email = array(
'email_1#xxx.com',
'email_2#xxx.com',
'email_3#xxx.com'
);
How could we possibly make the mandrill code to use $subscribers_email? Of course we can write the email addresses in the mandrill code like:
<?php
$mandrill_message = array(
// ...blah blah, doesn't matter
// To send to only one amil address:
'to' => array(
array(
'email' => 'email_1#xxx.com',
),
array(
'email' => 'email_2#xxx.com',
),
array(
'email' => 'email_3#xxx.com',
),
),
// ...blah blah, doesn't matter
);
?>
But that's not what I want, imagine we need to make this dynamic and as a function, like:
function sendEmailToArray($subscribers_email){
$mandrill_message = array(
// ...blah blah, doesn't matter
// To send to only one amil address:
'to' => array(
$subscribers_email
),
// ...blah blah, doesn't matter
);
}
So I want to make that array in a dynamic way, is that possible?
You should convert your array as your requirement.
Use this code
function sendEmailToArray($subscribers_email){
//$subscribers_email = array(
// 'email_1#xxx.com',
// 'email_2#xxx.com',
// 'email_3#xxx.com'
//);
$final_subscribers_email = array();
foreach ($subscribers_email as $key => $value) {
array_push($final_subscribers_email, array("email" => $value));
}
$mandrill_message = array(
// ...blah blah, doesn't matter
// To send to only one email address:
'to' => $final_subscribers_email,
// ...blah blah, doesn't matter
);
}
You can use array_walk for this.
$subscribers_email = array(
'email_1#xxx.com',
'email_2#xxx.com',
'email_3#xxx.com'
);
sendEmailToArray($subscribers_email);
function sendEmailToArray($subscribers_email){
// Convert the array
array_walk($subscribers_email, function(&$input){
$input = array('email' => $input);
});
// Send the emails
$mandrill_message = array(
'to' => array($subscribers_email),
);
}

How to send E-mail Templates through Mailgun?

I am using Mailgun as my e-mail Service provider for sending E-mail to my Colleagues. I have created some attractive E-mail Templates. But, I don't know how to send it through Mailgun. Kindly guide me...
this is simple to do if you're using the PHP library from mailgun, available here: https://github.com/mailgun/mailgun-php
The below code will send an email to a colleague of yours, you can add as many to fields as you need!
Then simply edit the array 'html' :) and execute the script!
# Include the Autoloader (see "Libraries" for install instructions)
require 'vendor/autoload.php';
use Mailgun\Mailgun;
# Instantiate the client.
$mgClient = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0l');
$domain = "YOURDomain.mailgun.org";
# Make the call to the client.
$result = $mgClient->sendMessage($domain, array(
'from' => 'You#yourdomain.com',
'to' => 'your_colleague#someone.com',
'subject' => 'Hello',
'html' => 'some html code <b> goes here </b> and works <span style=\"color:red\">fine</span>'
));
Or you can do a file read of the template and store it temporarily:
$fileloc = fopen("/path/to/email_template.php", 'r');
$fileread = fread($fileloc, filesize("/path/to/email_template.php"));
# Instantiate the client.
$mg = new Mailgun('<your_api_key>');
$domain = "domain.com";
# Make the call to the client.
$mg->sendMessage($domain, array('from' => 'noreply#domain.com',
'to' => 'recipient#domain.com',
'subject' => 'Subject',
'html' => $fileread));
Try this
$content = view('email.info-request',
['sender_name' => Input::get('sender_name'),
'sender_email' => Input::get('senderr_email'),
'sender_subject' => Input::get('sender_subject'),
'sender_message' => Input::get('sender_message')])->render();
$mg->messages()->send('mydomain.com', [
'from' => Input::get('sender_email'),
'to' => 'admin#mydomain.com',
'subject' => 'Information Request',
'html' => $content]);

Categories