I'm trying to send email using Mandrill and PHP and I can't get it to send.
I've downloaded the PHP API wrapper from here:https://packagist.org/packages/mandrill/mandrill
Mandrill.php is in my root and the Mandrill folder is in the same directory.
Here's my code:
<?php
require_once 'Mandrill.php';
$mandrill = new Mandrill('MY API KEY IS USUALLY HERE');
$message = array(
'subject' => 'Test message',
'from_email' => 'jwjody#yahoo.com',
'from_name' => 'Sender person',
'html' => '<p>this is a test message with Mandrill\'s PHP wrapper!.</p>',
'to' => array(array('email' => 'jwjody#yahoo.com', 'name' => 'Recipient 1')),
'merge_vars' => array(array(
'rcpt' => 'recipient1#domain.com',
'vars' =>
array(
array(
'name' => 'FIRSTNAME',
'content' => 'Recipient 1 first name'),
array(
'name' => 'LASTNAME',
'content' => 'Last name')
))));
//print_r($mandrill->messages->sendTemplate($template_name, $template_content, $message));
echo ("hello");
?>
But it won't send. I'm not sure where the failure is. Is it something obvious I'm missing?
I see the issue now.
I see what's going on now!
I changed
$mandrill->messages->sendTemplate($template_name, $template_content, $message));
to
$mandrill->messages->send($message, $async=false, $ip_pool=null, $send_at=null);
And it works!
Instead of calling the sendTemplate() function I should have used
$mandrill->messages->send($message, $async=false, $ip_pool=null, $send_at=null);
Once I changed the function call the mail was sent.
Related
I am having a problem usign the Mailchimp API with PHP.
When I'm running the code, this is the error I get:
{
"type": "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
"title": "Bad Request",
"status": 400,
"detail": "Your Campaign is not ready to send.",
"instance": "46235d29-6a67-4d55-800d-a95e2dc7273f"
}
I know why the campaign is not ready to send, as you can see in this image, the details are filled, but not saved:
Here is the code I'm trying to run:
<?php
require '../vendor/autoload.php';
$apiKey = 'API ';
$mailchimp = new \MailchimpAPI\Mailchimp($apiKey);
$list_created = $mailchimp->lists()->post([
'name' => 'Lista Monday',
'contact' => [
'company' => 'Test Company Monday',
'address1' => 'Monday address 1',
'city' => 'Monday City',
'state' => 'Monday State',
'zip' => '227560',
'country' => 'Romania'
],
'permission_reminder' => 'test',
'campaign_defaults' => [
'from_name' => 'Anjus Parsay',
'from_email' => 'FROM EMAIL',
'subject' => 'Monday Email',
'language' => 'English'
],
'email_type_option' => false
]);
$list_id = json_decode($list_created->getBody(), true)['id'];
$email = "EMAIL ";
$addedUser = $mailchimp->lists($list_id)->members()->post([
'email_address' => $email,
'status' => 'subscribed'
]);
//1. Create the template
$template_content = file_get_contents("http://widevisiondesign.com/anjus/mailchimp/src/testTemplate.html");
$template = $mailchimp->templates()->post([
'name' => 'testTemplateHTML',
'html' => $template_content
]);
$template_id = json_decode($template->getBody(), true)['id'];
//2. Create a campaign
$campaign = $mailchimp->campaigns()->post([
'type' => 'regular',
'recipients' => [
'list_id' => $list_id
],
'settings' => [
'title' => 'Monday',
'subject_line' => 'Monday - Sunday',
'from_name' => 'NAME NAME',
'to_name' => 'test',
'preview_text' => 'Preview text - Monday',
'template_id' => $template_id
]
]);
$campaign_id = json_decode($campaign->getBody(), true)['id'];
$sent = $mailchimp->campaigns($campaign_id)->send([$email], 'plaintext')->getBody();
print_r($sent);
?>
Thank you!
From the code and screenshot you presented, here are some things that came to my mind:
Have you verified and authenticated your email address? If no, you need to authenticate the domain of your email address. Go to your Mailchimp dashboard, click Account > Settings > Domains > Verify a Domain.
Then add your domain, follow the instruction to verify and authenticate.
If you prefer, you can make an API call to get a checklist before actually sending the email. The checklist gives a success and error notes on what needs to be done before actually sending the email.
I'm sorry if this seems somehow but did you really provide your email address in place of the 'FROM EMAIL' placeholder? From the code, it seems that you set the name of the from_name but did not set the from_email.
I guess there is a issue with Mandrill attachments x Hotmail/Outlook.com.
I can send e-mail attachments with Mandrill using base64_encode(). I am using the PHP library.
If I send an e-mail to Gmail or any other provider, the attachment looks fine. Downloading it and opening normally.
But sending to a Hotmail/Outlook.com, the attachments came with no extension/file format. By downloading, it can only be opened by adding the extension manually (for example, .pdf).
The code:
$file_data = file_get_contents($_FILES["test_file"]["tmp_name"])
$file_type = $_FILES["test_file"]["type"]
$mandrill = new Mandrill('MANDRILL_API_KEY_HERE');
$message = array(
'html' => $html,
'subject' => 'Testing',
'from_email' => 'sender#test.com',
'from_name' => 'Sender',
'attachments' => array(
array(
'name' => 'Test PDF document',
'type' => $file_type,
'content' => base64_encode($file_data)
)
),
'to' => array(
array(
'email' => 'to#test.com',
'name' => 'John Doe',
'type' => 'to'
)
),
'headers' => array('Reply-To' => 'sender#test.com')
);
$result = $mandrill->messages->send($message);
Any ideas? Is it a Mandrill or Hotmail issue?
Thanks!
I have found the solution. Gmail and other email services recognizes the type attribute as above, but Hotmail only recognizes the file type by adding the extension to the attachment name (name attribute).
For example:
'name' => 'Test PDF document.pdf',
It look obvious, but Hotmail requires it. Sharing my simple knowledge, since I received no answers.
This issue really driving me crazy this past hours. Why is the email that send with Mandrill not showing the right information about the Cc email address.
So for example, I want to send email to
receiver_to_a#mail.com
receiver_cc_a#mail.com
receiver_cc_b#mail.com
When I see the email header on receiver_cc_a#mail.com. its always show no cc, and that email is send "to" receiver_cc_a#mail.com, not as cc
Is anyone having the same problem? I already try sending email in PHP with PhpMailer and also try from the PHP API from Mandrill itself but no luck.
You need to set the option preserve_recipients to true.
$message = array(
'html' => '<p>Example HTML content</p>',
'text' => 'Example text content',
'subject' => 'example subject',
'from_email' => 'message.from_email#example.com',
'from_name' => 'Example Name',
'to' => array(
array(
'email' => 'recipient.email#example.com',
'name' => 'Recipient Name',
'type' => 'to'
),
array(
'email' => 'ccrecipient.email#example.com',
'name' => 'Recipient Name',
'type' => 'cc'
)
),
'headers' => array('Reply-To' => 'message.reply#example.com'),
'preserve_recipients' => true
);
I want to send an email using zend/mail in zend framework 2.
I already have some code but i don't know where to put it, and also not how to trigger this.
$mail = new Mail\Message();
$mail->setBody('This is the text of the email.');
$mail->setFrom('email#hotmail.be', 'email');
$mail->addTo('email#hotmail.be', 'email');
$mail->setSubject('Dit is een email verzonden van de computer');
$transport = new Mail\Transport\Sendmail('-freturn_to_email#hotmail.be');
$transport->send($mail);
I still new at Zend framework 2.
Can anybody help me with this?
I would advice you to take a look at Soflomo\Mail. Note, I am the author of Soflomo mail, but it helps you a lot with sending mails. It eases the config and dependency hassle.
Put "soflomo/mail": "~0.3" in your composer.json file and run the following command:
php composer.phar update soflomo/mail
Next, enable the module Soflomo\Mail in your application.config.php. When you use Zend\Mvc, you will have a controller/action which should trigger the email. For the most simplified use case, you can do this:
public function doAction()
{
// some of your logic
$service = $this->getServiceLocator()->get('Soflomo\Mail\Service\MailService');
$service->send(array(
'to' => 'email#hotmail.be',
'to_name' => 'email',
'from' => 'email#hotmail.be',
'from_name' => 'email',
'subject' => 'Dit is een email verzonden van de computer',
'template' => 'mail/message/default'
));
}
Now Soflomo\Mail sends a message by rendering a template and use that as message text. Here I defined a message mail/message/default so create that file (e.g. module/Application/view/mail/message/default.phtml) with this content:
This is the text of the email.
The last thing to do is to configure how Soflomo/Mail sends the message. Your question uses Sendmail, so I am using this as well in this example. Create a configuration file in config/autoload , e.g. config/autoload/soflomo_mail.global.php which contains the following content:
return array(
'soflomo_mail' => array(
'transport' => array(
'type' => 'sendmail',
),
),
);
If you want to switch to e.g. GMail as transport layer, replace above config with:
return array(
'soflomo_mail' => array(
'transport' => array(
'type' => 'smtp',
'options' => array(
'name' => 'gmail.com',
'host' => 'smtp.gmail.com',
'port' => 587,
'connection_class' => 'login',
'connection_config' => array(
'ssl' => 'tls',
'username' => '%USERNAME%',
'password' => '%PASSWORD%',
),
),
'variables' => array(
'username' => '',
'password' => '',
),
),
),
);
And create a new file config/autoload/soflomo_mail.local.php:
return array(
'soflomo_mail' => array(
'transport' => array(
'variables' => array(
'username' => 'my-address#gmail.com',
'password' => '1234secure7890',
),
),
),
);
I guess Hotmail would be similar to GMail.
You can use this within the zend directory,
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('somebody#example.com', 'Some Sender');
$mail->addTo('somebody_else#example.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send();
So I've made a template called 'Basic' within Mandrill. I did a test send and it looks great. I put Mandrill into test mode and used the test API key in my code. I'm just trying to get the php to send out a test transactional email, but no email gets sent. Here is the printed response I get:
Array ( [0] => Array ( [email] => amiecrutchley02#gmail.com [status] => sent [_id] => 89bfab4c3938486eb9e36564f79a3e9f [reject_reason] => ) )
So I'm really not sure why I'm not receiving anything.
Here is my code:
<?php
require_once('includes/mandrill/Mandrill.php');
$mandrill = new Mandrill('my_api_key');
$message = array(
'subject' => 'Thank You For Your Purchase',
'from_email' => 'no-reply#acq.com',
'from_name' => 'ACQ',
'to' => array(array('email' => 'amiecrutchley02#gmail.com', 'name' => 'Amie')),
'merge_vars' => array(array(
'rcpt' => 'amiecrutchley02#gmail.com',
'vars' =>
array(
array(
'name' => 'FIRSTNAME',
'content' => 'Amie'),
array(
'name' => 'LASTNAME',
'content' => 'Crutchley')
))));
$template_name = 'Basic';
$template_content = array(
array(
'name' => 'main',
'content' => 'Hi *|FIRSTNAME|* *|LASTNAME|*, your profile has been updated!'),
array(
'name' => 'footer',
'content' => 'ACQ, Copyright 2014')
);
$response = $mandrill->messages->sendTemplate($template_name, $template_content, $message);
print_r($response);
?>
a test API key is specifically designed to not send email. It's designed so you can mimic sending email, but doesn't actually send. You're also not charged for sending tests. Here's the Mandrill KB about what test mode is, and how it works: Does Mandrill have a test mode or sandbox?
Well, weird, but my test API was the problem. I tried the public API and boom! It works!