I'll try to be clear:
I'm working on a web app using amazon EC2, Amazon S3, Amazon RDS and now Amazon SES.
I'm trying to implement email verification upon sign up. Unfortunately the php mail function doesn't work on amazon EC2 instances for some reason, I have tried reinstalling sendmail and postfix and looking into the php.ini file to no avail. Which leads me to following this: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-sdk-php.html
that works fine but only if you run it from the shell accessed via ssh using putty.
Using username "ec2-user".
Authenticating with public key "imported-openssh-key"
Last login: Fri Aug 7 05:32:51 2020 from 116.90.72.92
__| __|_ )
_| ( / Amazon Linux AMI
___|\___|___|
https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/
[ec2-user#ip-172-31-24-178 ~]$ sudo su
[root#ip-172-31-24-178 ec2-user]# php amazon-ses-sample.php
Email sent! Message ID: 010f0173c771a9d3-b9d5e167-7b19-4b42-a550-dce52a8b83d5-000000
[root#ip-172-31-24-178 ec2-user]#
All my php scripts are run from the apache (httpd) server running on an EC2 instance that I'm using. The scripts run fine, but I trying to include the amazon ses sample(see code below) it errors out and doesn't execute:
//include 'amazon-ses-sample.php'; <-------**HERE: including this doesn't work.**
?>
This script is from AWS which only work if you use the command: $ php amazon-ses-sample.php
// If necessary, modify the path in the require statement below to refer to the
// location of your Composer autoload.php file.
require '/var/www/aws/aws-autoloader.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-2'
]);
// Replace sender#example.com with your "From" address.
// This address must be verified with Amazon SES.
$sender_email = 'ngudy049#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 = ['ngudy049#gmail.com','ngudy049#gmail.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';
$nameString = "username3";
$subject = 'Amazon web app sign up test';
$plaintext_body = "This email was sent with Amazon SES using the AWS SDK for PHP.".
"Username: ".$nameString." Password: ".$passwordString."Please click this link to activate your account:".
"http://www.yourwebsite.com/verify.php?email=".$emailString."&hash=".$hashString; // Our message above including the link
$html_body = "<p>Thanks for signing up! Your account has been created, you can login".
" with the following credentials after you have activated your account by pressing the url below.</p>".
"<br>".
"Username: ".$usernameString. "<br>".
"Password: ".$passwordString. "<br>".
"<br>".
"http://3.23.88.212/verify.php?email=".$emailString."&hash=".$hashString; // Our message above including the link;
$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";
}
?>
I have tried adding the contents of amazon-ses-sample.php to my php scripts but that doesn't work. It only works if you use:
[root#ip-172-31-24-178 ec2-user]# php /var/www/html/amazon-ses-sample.php
Judging based on your code I see the following observations.
When you run /var/www/html/amazon-ses-sample.php it works, but when you call as root on the CLI but otherwise for the apache user it does not work.
As you're running using include this will simply raise a warning to highlight the file cannot be included (so make sure to check your local PHP logs), whereas require will explicitly fatal error if it cannot reach the file.
There are a number of issues that could cause this but here are the likely ones:
Check file path relative to the script, if you simply call include file.php this will be added relative to the current directory.
Ensure the local file permissions of the file allow the apache user/group to read and execute the file. This properties can vary per OS/installation and can be set as custom values.
Related
I am attempting to send an email through AWS SES using the php sdk. Please note that I am able to send a message through the aws CLI - no problem, and I am able to send an email from the SES console using the domain and email areas under Identity Management area. Also, both the domain and email have been verified and appear in the Identity Management area.
I am using the example code from the PHP SDK. I did edit the region as my emails and domains have been verified, and I operate out of the us-west-2 region.
<?php
/**
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* This file is licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License. A copy of
* the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
require 'vendor/autoload.php';
use Aws\Ses\SesClient;
use Aws\Exception\AwsException;
$SesClient = new Aws\Ses\SesClient([
'profile' => 'default',
'version' => '2010-12-01',
'region' => 'us-west-2'
]);
$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>';
$subject = 'Amazon SES test (AWS SDK for PHP)';
$plaintext_body = 'This email was send with Amazon SES using the AWS SDK for PHP.';
$sender_email = 'email_address';
$recipient_emails = ['email_address'];
$char_set = 'UTF-8';
$configuration_set = 'ConfigSet';
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,
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
echo $e->getMessage();
echo "\n";
}
I am providing the credentials for the SDK through the ~/.aws/credential file, that contains the access ID key and secret key. This is the same file that is accessed by the AWS CLI, so the credentials appear to be correct and accessible to the CLI.
[default]
aws_access_key_id = AKIA2PK........Z72J3
aws_secret_access_key = co9r41j/i+QTrsA7244xG........KPH1LyBn34b
I added the "........" to the above to protect my access and secret access keys.
I get the following error that indicates that my email has not been verified. But it has! Also, I am not in the sandbox mode.
"Error executing "SendEmail" on "https://email.us-west-2.amazonaws.com"; AWS HTTP error: Client error: POST https://email.us-west-2.amazonaws.com resulted in a 400 Bad Request response: Sender MessageReje (truncated...) MessageRejected (client): Email address is not verified. The following identities failed the check in region US-WEST-2: msellers#ranchmed.com - Sender MessageRejected Email address is not verified. The following identities failed the check in region US-WEST-2: msellers#ranchmed.com dbbb0c1d-5493-4205-9257-a2fbb81eb5d9 The email was not sent. Error message: Email address is not verified. The following identities failed the check in region US-WEST-2: msellers#ranchmed.com"
Any ideas on how to debug this?
Appreciate any help that you can provide.
Mark
Found the problem. I hope this helps other aws sdk users. The sdk credential credentials provider found an .aws/credentials file for the webserver user and not from my home directory. In my case, looked in the file /var/cache/nginx/.aws/credentials and found some credentials defined for previous testing purposes.
Lesson is, be aware of how the aws sdk finds the credentials file. If the web server user is set up with a credentials file, then it will use that credential file in preference to one located in your $HOME/.aws/credentials file.
I'm trying to receive e-mails sended to my account on sendgrid.
So What I basically have is the following:
A sendgrid account linked to http://www.rallypodium.be
A cloudflare account linked to http://www.rallypodium.be server IP adress.
A server on DigitalOcean.
Good, So I'm running Nginx on that server and I want to be able to get the e-mails send to #rallypodium.be to be saved and stored inside of my database wich is on the same DigitalOcean server.
I've set up the Inbound Parse like this:
HOST: www.rallypodium.be
URL: http://www.rallypodium.be/inbound/parse/mail
My domain is whitelabled.
I've read the docs for 10 times and still didn't figure out what I'm doing wrong.
This is how I store them:
public function ReceiveMail(Request $request)
{
DB::table('email')->insert([
'headers' => $request->get('headers'),
'html' => $request->get('html'),
'from' => $request->get('from'),
'to' => $request->get('to'),
'cc' => $request->get('cc'),
'subject' => $request->get('subject'),
'dkim' => $request->get('dkim'),
'spf' => $request->get('spf'),
'envelope' => $request->get('envelope'),
'charsets' => $request->get('charsets'),
'spam_score' => $request->get('spam_score'),
'spam_report' => $request->get('spam_report'),
'attachments' => $request->get('attachments'),
'attachment-info' => $request->get('attachment-info'),
'attachmentX' => $request->get('attachmentX')
]);
return 'ok';
}
If I take a look at the Activity Feed, then I see this:
The error message is the following:
EMAIL: robin#rallypodium.be
REASON: error dialing remote address: dial tcp 104.24.101.114:25: i/o timeout
SMTP-ID: <1f7f313f27fd051b525581562e6af9b5#rallypodium.be>
PROCESSED STRING: August 1, 2016 - 06:53:45PM
MSGID: J1irmehmR_GELI7tIpPXNg.filter0810p1mdw1.1861.579F77CC27.0
oh and this is my cloudflare DNS: http://prntscr.com/c0bjl8
Can someone help me out?
Thanks!
You are essentially trying to receive email through CloudFlare, but unfortunately CloudFlare doesn't proxy SMTP/email traffic.
Instead you'll need to add a grey-clouded record to manage your email, this will allow your email to be routed straight to your origin without CloudFlare blocking it. Note that grey clouded domains can reveal your IP Address, it is therefore recommended to have your email server on a separate server to your webserver; or even better use a Cloud email provider and get emails from them.
My EC2 servers are currently hosting a website that logs each registered user's activity under their own separate log file on the local EC2 instance, say username.log. I'm trying to figure out a way to push log events for these to CloudWatch using the PHP SDK without slowing the application down, AND while still being able to maintain a separate log file for each registered member of my website.
I can't for the life of me figure this out:
OPTION 1: How can I log to CloudWatch asynchronously using the CloudWatch SDK? My PHP application is behaving VERY sluggishly, since each log line takes roughly 100ms to push directly to CloudWatch. Code sample is below.
OPTION 2: Alternatively, how could I configure an installed CloudWatch Agent on EC2 to simply OBSERVE all of my log files, which would basically upload them asynchronously to CloudWatch for me in a separate process? The CloudWatch EC2 Logging Agent requires a static "configuration file" (AWS documentation) on your server which, to my knowledge, needs to lists out all of your log files ("log streams") in advance, which I won't be able to predict at the time of server startup. Is there any way around this (ie, simply observe ALL log files in a directory)? Config file sample is below.
All ideas are welcome here, but I don't want my solution to simply be "throw all your logs into a single file, so that your log names are always predictable".
Thanks in advance!!!
OPTION 1: Logging via SDK (takes ~100ms / logEvent):
// Configuration to use for the CloudWatch client
$sharedConfig = [
'region' => 'us-east-1',
'version' => 'latest',
'http' => [
'verify' => false
]
];
// Create a CloudWatch client
$cwClient = new Aws\CloudWatchLogs\CloudWatchLogsClient($sharedConfig);
// DESCRIBE ANY EXISTING LOG STREAMS / FILES
$create_new_stream = true;
$next_sequence_id = "0";
$result = $cwClient->describeLogStreams([
'Descending' => true,
'logGroupName' => 'user_logs',
'LogStreamNamePrefix' => $stream,
]);
// Iterate through the results, looking for a stream that already exists with the intended name
// This is so that we can get the next sequence id ('uploadSequenceToken'), so we can add a line to an existing log file
foreach ($result->get("logStreams") as $stream_temp) {
if ($stream_temp['logStreamName'] == $stream) {
$create_new_stream = false;
if (array_key_exists('uploadSequenceToken', $stream_temp)) {
$next_sequence_id = $stream_temp['uploadSequenceToken'];
}
break;
}
}
// CREATE A NEW LOG STREAM / FILE IF NECESSARY
if ($create_new_stream) {
$result = $cwClient->createLogStream([
'logGroupName' => 'user_logs',
'logStreamName' => $stream,
]);
}
// PUSH A LINE TO THE LOG *** This step ALONE takes 70-100ms!!! ***
$result = $cwClient->putLogEvents([
'logGroupName' => 'user_logs',
'logStreamName' => $stream,
'logEvents' => [
[
'timestamp' => round(microtime(true) * 1000),
'message' => $msg,
],
],
'sequenceToken' => $next_sequence_id
]);
OPTION 2: Logging via CloudWatch Installed Agent (note that config file below only allows hardcoded, predermined log names as far as I know):
[general]
state_file = /var/awslogs/state/agent-state
[applog]
file = /var/www/html/logs/applog.log
log_group_name = PP
log_stream_name = applog.log
datetime_format = %Y-%m-%d %H:%M:%S
Looks like we have some good news now... not sure if it's too late!
CloudWatch Log Configuration
So to answer the doubt,
Is there any way around this (ie, simply observe ALL log files in a directory)?
yes, we can mention log files and file paths using wild cards, which can help you in having some flexibility in configuring from where the logs are fetched and pushed to the log streams.
I'm trying to use the WhatsApi Official library to send a message via WhatsApp from a php file. I've moved in my Apache web server the library, in a folder call test, like this:
The file whatsapp.php is this one:
<?php
require_once './src/whatsprot.class.php';
$username = "1XXXXXXXXX";
$password = "password";
$w = new WhatsProt($username, "0", "My Nickname", true); //Name your application by replacing “WhatsApp Messaging”
$w->connect();
$w->loginWithPassword($password);
$target = '1xxxxxxxxx'; //Target Phone,reciever phone
$message = 'This is my messagge';
$w->SendPresenceSubscription($target); //Let us first send presence to user
$w->sendMessage($target,$message ); // Send Message
echo "Message Sent Successfully";
?>
I'm facing some problem with the library new WhatsProt(), which blocks all the code (may be sockets ?).
So my question is, how can I fix this problem ? If no, are there any other solution to send message from a pho script ?
You can use below script to send message from whatsapp in PHP.
https://github.com/venomous0x/WhatsAPI/tree/master/examples
Configure the source code in Apache and run examples/whatsapp.php file.
You have change below configurations.
//simple password to view this script
$config['webpassword'] = 'MakeUpPassword';
and
$config['YOURNAME'] = array(
'id' => 'e807f1fcf82d132f9bb018ca6738a19f',
'fromNumber' => '441234567890',
'nick' => "YOURNICKNAME",
'waPassword' => "EsdfsawS+/ffdskjsdhwebdgxbs=",
'email' => 'testemail#gmail.com',
'emailPassword' => 'gmailpassword'
);
It's working for me..
afaik you are probably better off currently writing an interface to a python project. E.g. have a microservice that does sending of messages for you in python, and you call them via some json request or similar
see this project, looks promising: https://github.com/tgalal/yowsup
it seems like the only viable option so far, as everything else was shut down or has a high probability to get your account banned
see discussion here:
https://stackoverflow.com/a/46635985/533426
I am trying to send a message by the WhatsApp API using PHP. I have the WhatsApp password and am getting by WART using the following code:
<?php
require "whatsapp.class.php";
// DEMO OF USAGE
$wa = new WhatsApp("91XXXXXXXXXX", "XXX-XXX", "Nick Name");
$wa->Connect();
$t = $wa->Login();
$wa->Message("5","91XXXXXXXXXX","Good code");
echo "Message sent";
?>
I did not change anything in the whatsapp.class.php file.
My files are:
http://vvsindia.com/stackoverflow/whatsapp.class.txt
http://vvsindia.com/stackoverflow/func.txt
http://vvsindia.com/stackoverflow/decode.txt
For your convenience to view while browsing, I just uploaded them as a txt file, but originally these are PHP files.
Using the above code I was not able to send any message. What could the issue be?
You can use the below script to send a message from WhatsApp in PHP.
https://github.com/venomous0x/WhatsAPI/tree/master/examples
Configure the source code in Apache and run examples/whatsapp.php file.
You have to change the below configurations.
// Simple password to view this script
$config['webpassword'] = 'MakeUpPassword';
and
$config['YOURNAME'] = array(
'id' => 'e807f1fcf82d132f9bb018ca6738a19f',
'fromNumber' => '441234567890',
'nick' => "YOURNICKNAME",
'waPassword' => "EsdfsawS+/ffdskjsdhwebdgxbs=",
'email' => 'testemail#gmail.com',
'emailPassword' => 'gmailpassword'
);
You should rather try this quick and easy interface API:
https://www.mashape.com/motp/whatsapp-pusher
As given in the documentation, sending a text message to a WhatsApp user would be a one-step process. Below is a sample cURL call to send a text message to a WhatsApp user.
curl -XPOST 'http://api.dial2verify.com/WHAPP/SEND/<API_KEY>/<Phone_ISD>' \
-d 'Msg=Text to image URL here'
To get the API key, you are required to drop a request to hello#dial2verify.in, and they would provide you a free API key.
Phone_ISD: should be a complete phone number including ISD code (for example, 919922003300).