I am using Mandrill API to send email.
I have register my send domain and set my DKIM and SPF record properly inside Mandrill Setting page.
Following is my code that is being used to send email:
$template_name = "Test_Schedule_Reminder";
$to_email = "debesh#debeshnayak.com";
$to_name = "Test Email";
$from_email = "contact#debeshnayak.com";
$from_name = "Debesh Nayak";
require_once 'mandrill-api-php/src/Mandrill.php'; //Not required with Composer
try {
$mandrill = new Mandrill('my-mandrill-api-key');
$message = array(
'html' => $html_email_template,
'subject' => $email_title,
'from_email' => $from_email,
'from_name' => $from_name,
'to' => array(
array(
'email' => $to_email,
'name' => $to_name,
'type' => 'to'
)
),
'important' => true,
'track_opens' => true,
'track_clicks' => true,
'inline_css' => true,
'metadata' => array('website' => 'www.debeshnayak.com'),
);
$async = false;
$ip_pool = null;
$send_at = $utc_class_time;
$result = $mandrill->messages->send($message, $async, $ip_pool, $send_at);
print_r($result);
} catch(Mandrill_Error $e) {
// Mandrill errors are thrown as exceptions
echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
// A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123'
throw $e;
}
I am able to send email when I am sending from my production server.
But when I am trying to send email from localhost I am getting the following error:
Mandrill_HttpError - API call to messages/send failed: SSL certificate problem: unable to get local issuer certificate
So how to avoid this SSL certificate problem when testing mail from localhost using Mandrill API.
Do check library files of Mandrill and search for cURL call that sending out email. Check for "CURLOPT_SSL_VERIFYPEER" parameter of this cURL. Set value to false. It should help you.
I have added the following two line inside call() function of Mandrill.php library file:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
And now I am able to send email from my localhost using Mandrill API.
Related
I am using AWS SES on a website with shared hosting platform (not on AWS)
When I send emails using the SMTP it works perfectly. When I attempt to call and use the API, no email is sent, no error message is received.
There are two reasons I want to use the API instead of SMTP
1) According to Amazon, the API is faster.
2) One of my transactional emails is a reminder to clients to log in, and/or pay their account. SMTP cannot send email to more than 50 (if I am not mistaken). For that reason, I need to call the SendRawEmail.
Please help me with my code to send the email and to implement SendRawEmail
Here is my code:
require ('PHPMailer/PHPMailerAutoload.php');
use Aws\Ses\SesClient;
use Aws\Exception\AwsException;
// Create an SesClient. Change the value of the region parameter if you're
// using an AWS Region other than US West (Oregon). Change the value of the
// profile parameter if you want to use a profile in your credentials file
// other than the default.
$SesClient = new SesClient([
'profile' => 'default',
'version' => '2010-12-01',
'region' => 'eu-central-1'
]);
// Replace sender#example.com with your "From" address.
// This address must be verified with Amazon SES.
$sender_email = 'info#mydomain.com';
// Replace these sample addresses with the addresses of your recipients. If
// your account is still in the sandbox, these addresses must be verified.
$recipient_emails = ['info#example.com'];
// Specify a configuration set. If you do not want to use a configuration
// set, comment the following variable, and the
// 'ConfigurationSetName' => $configuration_set argument below.
//$configuration_set = 'ConfigSet';
$subject = 'Amazon SES test (AWS SDK for PHP)';
$plaintext_body = 'This email was sent with Amazon SES using the AWS SDK for PHP.' ;
$html_body = '<h1>AWS Amazon Simple Email Service Test Email</h1>'.
'<p>This email was sent with <a href="https://aws.amazon.com/ses/">'.
'Amazon SES</a> using the <a href="https://aws.amazon.com/sdk-for-php/">'.
'AWS SDK for PHP</a>.</p>';
$char_set = 'UTF-8';
try {
$result = $SesClient->sendEmail([
'Destination' => [
'ToAddresses' => $recipient_emails,
],
'ReplyToAddresses' => [$sender_email],
'Source' => $sender_email,
'Message' => [
'Body' => [
'Html' => [
'Charset' => $char_set,
'Data' => $html_body,
],
'Text' => [
'Charset' => $char_set,
'Data' => $plaintext_body,
],
],
'Subject' => [
'Charset' => $char_set,
'Data' => $subject,
],
],
// If you aren't using a configuration set, comment or delete the
// following line
'ConfigurationSetName' => $configuration_set,
]);
$messageId = $result['MessageId'];
echo("Email sent! Message ID: $messageId"."\n");
} catch (AwsException $e) {
// output error message if fails
echo $e->getMessage();
echo("The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n");
echo "\n";
}
?>
Check your error log. I had the same issue:
require 'AWS/aws-autoloader.php';
For other SDK uses (such as API calls) you may need to create a .aws folder to include credentials
I am using the AWS example to call the AWS SDK for PHP. When converting the code to a function I get the following error: Uncaught Error: Call to a member function sendEmail()
line 41 seems to be the issue: $result = $SesClient->sendEmail([
I have tried removing the result variable and commenting out its usage
When I run the code and its not a function its working fine, I am not sure what I am doing wrong here, and I am sure it could be a simple error.
Thanks for the help in advance
<?php
// If necessary, modify the path in the require statement below to refer to the
// location of your Composer autoload.php file.
require '/vendor/autoload.php';
use Aws\Ses\SesClient;
use Aws\Exception\AwsException;
// Create an SesClient. Change the value of the region parameter if you're
// using an AWS Region other than US West (Oregon). Change the value of the
// profile parameter if you want to use a profile in your credentials file
// other than the default.
$SesClient = new SesClient([
'profile' => 'default',
'version' => '2010-12-01',
'region' => 'us-east-1'
]);
function send_email($recipient, $message, $subject){
// Replace sender#example.com with your "From" address.
// This address must be verified with Amazon SES.
$sender_email = 'sender#gmail.com';
// Replace these sample addresses with the addresses of your recipients. If
// your account is still in the sandbox, these addresses must be verified.
// $recipient_emails = ['recipient1#example.com','recipient2#example.com'];
$recipient_emails = [$recipient];
// Specify a configuration set. If you do not want to use a configuration
// set, comment the following variable, and the
// 'ConfigurationSetName' => $configuration_set argument below.
// $configuration_set = 'ConfigSet';
$subject = $subject;
$plaintext_body = $message.PHP_EOL.'This email was sent with Amazon SES using the AWS SDK for PHP.' ;
$html_body = '<h1>'.$message.'</h1>';
$char_set = 'UTF-8';
try {
$result = $SesClient->sendEmail([
'Destination' => [
'ToAddresses' => $recipient_emails,
],
'ReplyToAddresses' => [$sender_email],
'Source' => $sender_email,
'Message' => [
'Body' => [
'Html' => [
'Charset' => $char_set,
'Data' => $html_body,
],
'Text' => [
'Charset' => $char_set,
'Data' => $plaintext_body,
],
],
'Subject' => [
'Charset' => $char_set,
'Data' => $subject,
],
],
// If you aren't using a configuration set, comment or delete the
// following line
// 'ConfigurationSetName' => $configuration_set,
]);
$messageId = $result['MessageId'];
echo("Email sent! Message ID: $messageId"."\n");
} catch (AwsException $e) {
// output error message if fails
echo $e->getMessage();
echo("The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n");
echo "\n";
}
return 'success';
}
echo send_email('test#gmail.com', 'test', 'test subject');
Try declaring $SesClient inside of the send_email function
It's working without the function as you've declared $SesClient outside the function. Declare it after function - or pass it to the function via
function function send_email($recipient, $message, $subject, $SesClient) {
/* snip */
}
echo send_email('test#gmail.com', 'test', 'test subject', $SesClient);
I am a complete noob in AWS. I configured AWS SES today and now I am able to send an email to a recipient using this code.
<?php
// Replace sender#example.com with your "From" address.
// This address must be verified with Amazon SES.
define('SENDER', 'sender email');
// Replace recipient#example.com with a "To" address. If your account
// is still in the sandbox, this address must be verified.
define('RECIPIENT', 'recipient email');
// Replace smtp_username with your Amazon SES SMTP user name.
define('USERNAME','my username');
// Replace smtp_password with your Amazon SES SMTP password.
define('PASSWORD','my password');
// If you're using Amazon SES in a region other than US West (Oregon),
// replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP
// endpoint in the appropriate region.
define('HOST', 'email-smtp.us-west-2.amazonaws.com');
// The port you will connect to on the Amazon SES SMTP endpoint.
define('PORT', '587');
// Other message information
define('SUBJECT','Hello from Driffle!');
define('BODY','Test Email');
require_once 'Mail.php';
$headers = array (
'From' => SENDER,
'To' => RECIPIENT,
'Subject' => SUBJECT);
$smtpParams = array (
'host' => HOST,
'port' => PORT,
'auth' => true,
'username' => USERNAME,
'password' => PASSWORD
);
// Create an SMTP client.
$mail = Mail::factory('smtp', $smtpParams);
// Send the email.
$result = $mail->send(RECIPIENT, $headers, BODY);
if (PEAR::isError($result)) {
echo("Email not sent. " .$result->getMessage() ."\n");
} else {
echo("Email sent!"."\n");
}
?>
But when I try to send an html formatted email. The output email returns plain text. Looking for a solution to send html emails through Amazon SES.
Found it. I was doing it a bit wrong, but this works...
Add these headers:
'Mime-Version' => '1.0',
'Content-Type' => 'text/html; charset="ISO-8859-1"',
Then submit the body as HTML markup (keep it simple as possible to begin with - it does not need to be a "page" necessarily).
bh
added content-type in $headers . It worked for me
require_once 'Mail.php';
$headers = array (
'Content-Type' => "text/html; charset=UTF-8", // <- add this line
'From' => SENDER,
'To' => RECIPIENT,
'Subject' => SUBJECT);
I've been creating this project, where the system accepts registration and then send the information thru mail to the owner. I'm using PEAR: Mail Mime to do this. Sending of mail is actually working on my local computer, but after uploading this project to a hosting site (GoDaddy), it's not sending anymore.
I did an error check to see what hinders the sending of mail, and I received this message:
Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]
I'm using a GMail account to send email messages, and use this code to do it:
/* INCLUDE PREREQUISITES IN ORDER TO SEND AN EMAIL */
include('../../application/third_party/Mail-1.3.0/Mail-1.3.0/Mail.php');
require_once '../../application/third_party/Mail_Mime-1.10.0/Mail_Mime-1.10.0/Mail/mime.php';
/* HEADER INFORMATION */
$from = '<***#gmail.com>';
$to = '<***#gmail.com>';
$subject = 'Someone registers';
$crlf = "\r\n";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
// CREATING THE MIME MESSAGE
$mime = new Mail_mime($crlf);
$html = '
<html>
<body>
Message here
</body>
</html>';
// SETTING THE BODY OF THE EMAIL TO HTML
$mime->setHTMLBody($html);
$body = $mime->get();
$headers = $mime->headers($headers);
// SENDER INFORMATION
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => '***#gmail.com',
'password' => '***'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
$errormessage = '<p>'.$mail->getMessage().'</p>'; // ERROR MESSAGE
$mail->getUserInfo();
}
Yes, I turned on the Access for less secure apps of the GMail sender account.
And yes, the error displaying is turned on.
ini_set('display_errors', 1);
error_reporting(E_ALL);
Any solutions that I might have missed?
Found an answer regarding the outgoing mail in this - Sending email through gmail SMTP on GoDaddy.
It is because of the hosting provider, GoDaddy, which restricts the SSL port of GMail (465), and other ports such as 25 and 587. Thanks to this article -Troubleshooting PHPMailer Problems - of GitHub for clearing that up. Users are resorted to use the mail-server of GoDaddy instead.
But I still managed to send an email using GMail by doing:
$smtp = Mail::factory('smtp');
/* SETTINGS WILL BE IN DEFAULT (E.G. HOST:LOCALHOST, PORT: 45) */
instead of
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => '***#gmail.com',
'password' => '***'
));
But the receiver receives the sent email as "spam" - result will be that the receiver won't notice the incoming emails from the system (not a good choice). And there are no records in the Sent items of the sender (means you "used" the email to send an email, but not entirely).
So, we are back with the mail-server of GoDaddy. First, you have to determine the relay server of your hosting account by checking this What is the name of my hosting account's relay server?, which was published by GoDaddy themselves. In my case, Linux (cPanel), which I'll be using localhost as my host.
$smtp = Mail::factory('smtp', array(
'host' => 'localhost',
'auth' => true,
'username' => '***#***.com',
'password' => '***'
));
But the weird thing is, there are still no records of sent items. Hope a GoDaddy representative reads this.
I'm using sendgrid to send mails from my app, all is fine until I try and add an attachment (a pdf generated within the page and stored as a variable).
The pdf sends if I use the standard php mail function however I want to add this to my sendgrid mail, I'm using the following code but am not having any luck:
$sendgrid = new SendGrid('SENDGRID_KEY');
$email = new SendGrid\Email();
$email
->addTo('example#mail.com')
->addBcc('example#mail.com')
->setFrom('example#mail.com')
->setSubject('Example')
->setFiles($pdfdoc)
->setHtml($example_html);
$sendgrid->send($email);
echo "you just sent a mail! <br>";
I've tried ->setsetFiles() and ->setAttachment() but neither seems to work and I get the following error message:
[09-Sep-2016 03:55:19 UTC] PHP Fatal error: Uncaught exception 'Guzzle\Common\Exception\InvalidArgumentException' with message 'Unable to open %PDF-1.4
3 0 obj
does anyone have any idea of how to do this?
`#Solved with using web api v2 Curl version like this
$fileName = 'filename.pdf';
$image_data = file_get_contents('gs://my-bucket/filename.pdf');
#sending part
$params = array(
'api_user' => $user,
'api_key' => $pass,
'x-smtpapi' => json_encode($json_string),
'to' => 'email#yourdomain.com',
'subject' => 'your subject',
'html' => 'testing body',
'text' => 'testing body',
'from' => 'yourmail#yourdomain.com',
'files['.$fileName.']' => $image_data
);
$request = $url.'api/mail.send.json';