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);
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'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 have a bit of a strange one. Using PHP Pear Mail I am sending an HTML email with a link in to a subdomain http://mysub.mydomain.co.uk
$body='<html><body><strong>Hello '.$forename.'</strong><br><br>Thank you for registering your details. To complete the process, please follow the link below in this email.<br><br>
Complete Verification Here</body></html>';
$headers = array ('From' => $from,'To' => $to,'Subject' => $subject);
$mime = new Mail_mime();
$mime->setHTMLBody($body);
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password)
);
$mail = $smtp->send($to, $headers, $body);
The email gets sent fine and if I print the body of the email onto the screen from the page sending the email the link works fine. However when it arrives in an email in MS outlook it is taking the first 2 characters out of myvalue in the link. If myvalue=12345678 it says myvalue=345678 and displays the link incorrectly for example it shows the above as ttp://mysub.mydomain.co.uk/?V=345678. Notice it removes the H in the http address and also adds a forward slash before the ?v= and the first 2 digits 12 are also missing. It then fails to open the link as it is displaying it incorrectly as a http link. The email also does not arrive as an HTML email at Gmail and there is no link.
Any idea what I am missing here?
Eventually found the issue
In the body text I had to replace every occurrence in the body of
"
with ASCII code
'
Now working fine
You don't have to manipulate the body text outside of the mail_mime package, you need to mime encode the body with it:
$mime = new Mail_mime();
$mime->setHTMLBody($body);
$mimebody = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory(
'smtp',
[
'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password,
'port' => $port
]
);
// send email
$mail = $smtp->send($to, $headers, $mimebody);
I'm having a problem sending emails out through SMTP using Amazons SES. My code is relatively simple and looks like this:
require_once "Mail.php";
require_once 'Mail/mime.php';
$stmp_info= array (
'host' => 'ssl://email-smtp.us-east-1.amazonaws.com',
'port' => 465,
'auth' => true,
'username' => 'xxxxxx',
'password' => 'xxx'
);
$headers = array (
'From' => 'sender#example.com',
'To' => 'test#example.com',
'Subject' => 'Test Message'
);
$mime = new Mail_mime("\n");
$mime->setTXTBody('Hello World');
$mime->setHTMLBody('<p>Hello World</p>');
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp', $stmp_info);
$mail = $smtp->send('test#example.com', $headers, $body);
The problem is I always get this error:
Failed to connect to email-smtp.us-east-1.amazonaws.com:465 [SMTP: Invalid response code received from server (code: -1, response: )]
But this works for testing a connection:
[root#job-server-1 ~]# $ echo quit | nc -v email-smtp.us-east-1.amazonaws.com 25
-bash: $: command not found
Connection to email-smtp.us-east-1.amazonaws.com 25 port [tcp/smtp] succeeded!
220 email-smtp.amazonaws.com ESMTP SimpleEmailService-376766033
Yet if I ping the address, it does not work. I've tried port 25, 465 and 587 for both tls and ssl. The weird part is it works on my local host and this error only occurs when I try to connect from my rackspace instance. Does anyoen have an idea of what can be causing this?
Verify your network setting with your instance. If you are using VPC , please do check your subnet and routing policies.
Its blocking to connect ses smtp from your instance.
Also fill Request to Remove Email Sending Limitations to remove some restrictions on port 25
hope it helps !!
I'm using Pear PHP Mailer to send an email through SMTP to a number of recipients, but for each one, it sends an additional email to the sender's email address as well.
This is my code: Any help is appreciated.
...
$from = "$from_email_name <$from_email_addr>";
$html_body = "$html";
$crlf = "\r\n";
$hdrs = array(
'From' => $from,
'Subject' => $subject
);
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setTXTBody($text_body);
$mime->setHTMLBody($html_body);
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail = Mail::factory('mail',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail->send($to_email_addr, $hdrs, $body);
....
Also, on the email it is sending to my sender address, its saying the correct recipient name and sender information, but its like the sender is getting a copy of each one it sends out.
It could be a "service" of your mail server provider to automatically put mails sent via the SMTP server in your IMAP/POP mail box. Ask him if that's the case.