Hello, I am working on my first app and I would like to have a contact form send an email from my app, but I do not wish to use the devices native mail client.
Ideally once the contact form is filled in and ‘submit’ is pressed, then the email is sent and a thank you message is displayed.
I can get this working in php but having trouble within the app.
Send an ajax request to the php script to send the mail
//PHP
//WARNING! This is pseudocode not real working code
//file /ajax/sendMail.php
$subject = $_POST['subject'];
$body= $_POST['body'];
sendMail($subject, $body);
exit('ok');
//Controller
//WARNING! This is pseudocode not real working code
angular.controller("myController", function($http) {
$scope.myEmailForm = {subject: 'MySubject', body: 'My email body'};
$http.post("http://www.example.com/ajax/sendMail.php", $scope.myEmailForm)
.success(function(response) { //Foo do something })
.error(function() { //Somethign fail; });
You might also look into using a third party SaaS, like Mandrill, SendGrid, or MailGun. I have worked w/ building in an email feature using Mandrill. Account is free to setup and threshold for billing on messaging is like 12k/month. Sending is just a matter of following their API to HTTP POST to their REST interface. Small example below:
.controller('PlaylistCtrl', function($scope, $http, $stateParams) {
//attach sendMail f() to the controller scope
$scope.sendEmail = _sendEmail;
//define the mail params as JSON, hard coded for sample code
// update JSON to reflect message you want to send
var mailJSON ={
"key": "...YOUR_KEY_HERE...",
"message": {
"html": "<p>Example HTML content</p>",
"text": "Example text content",
"subject": "example subject",
"from_email": "sender@sending.domain.com",
"from_name": "Support",
"to": [
{
"email": "user@receiving.domain.com",
"name": "John Doe",
"type": "to"
}
],
"important": false,
"track_opens": null,
"track_clicks": null,
"auto_text": null,
"auto_html": null,
"inline_css": null,
"url_strip_qs": null,
"preserve_recipients": null,
"view_content_link": null,
"tracking_domain": null,
"signing_domain": null,
"return_path_domain": null
},
"async": false,
"ip_pool": "Main Pool"
};
//reference to the Mandrill REST api
var apiURL = "https://mandrillapp.com/api/1.0/messages/send.json";
//used to send the email via POST of JSON to Manrdill REST API end-point
function _sendEmail() {
$http.post(apiURL, mailJSON).
success(function(data, status, headers, config) {
console.log('successful email send.');
console.log('status: ' + status);
}).error(function(data, status, headers, config) {
console.log('error sending email.');
console.log('status: ' + status);
});
}
})
Hi @PopcornDesign. I saw title of the topic and want to send email with php like you did. But I have an error when try to connect with the server. (php file). I think, could you maybe help me solve it.