Sending mail through a form

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.

Any advice greatly appreciated :smile:

Thanks
Ross

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; });
1 Like

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);
			});
	}
})
1 Like

Thank you Jeffleus, i’ve used Mandrill before for another web project and its great, a lot nicer than having my server send all these emails.

Thanks for the code example

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.

This is my angular code:

var app = angular.module(‘starter’, [‘ionic’]);

app.controller(‘send_email’,function($scope,$http){

var config = {
    method:'POST',
    url:'http://www.hobbylacuesta.com/correo.php',
    data:'correo=alexis.970991@gmail.com',
    headers:{'Content-Type':'application/x-www-form-urlencoded'}
}

$scope.sendemail = function(){
    var response = $http(config);
    
    response.success(function(data,status){
        console.log('Done');
    });
    
    response.error(function(data,status){
        console.log('Error');
    });
}

});

This es my php file (It is in the server):

<? php

$message = "email: ".$_POST[“correo”];
$message = htmlspecialchars($mensaje);
$message = stripslashes($mensaje);

$to = “alexis.970991@gmail.com”;
$subject = “You have new email”;
$header = “From:alexis.970991@gmail.com”;
$enviar = mail($to,$subject,$message,$header);

?> 

And this is the error.
image

Do you believe could you help me? … Thanks a lot! :grin:

put this in the top of your php file

header(‘Access-Control-Allow-Origin: *’);

Thank you @jeffleus, with your code I was able to implement my contact form using Mandrill.