0

I am trying to send a simple automated email to a recipient using php mail function. Maybe I missed something but the recipient is not receiving any mail in the inbox or spam despite saying that it was successfully sent. What do I do?

import React from 'react';
import { View, Button, StyleSheet } from 'react-native';
import axios from 'axios';

const App = () => {
  const sendEmail = async () => {
    try {
      const response = await axios.post(
        'http://mywebsite.000webhostapp.com/sendEmail.php',
        {
          recipientEmail: 'example@gmail.com',
          subject: 'Subject of the Email',
          message: 'Content of the email.',
        }
      );

      console.log('Email sent successfully');
      console.log("RESPONSE RECEIVED: ", response.data);
    } catch (error) {
      console.error('Error sending email:', error);
    }
  };

  return (
    <View style={styles.container}>
      <Button title="Send Email" onPress={sendEmail} />
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
});

export default App;

<?php
    $data = json_decode(file_get_contents('php://input'), true);
    
    $recipientEmail = $data['recipientEmail'];
    $subject = $data['subject'];
    $message = $data['message']; 
    
    $appPassword = 'appPassword';
    
    // Configure email headers
    $to = $recipientEmail;
    $subject = $subject;
    $headers = "From: noreply.myAppName@gmail.com\r\n";
    $headers .= "Reply-To: noreply.myAppName@gmail.com\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
    
    // Use sender's email in the mail function
    $mailSuccess = mail($to, $subject, $message, $headers, '-f' . $appPassword);
    
    if ($mailSuccess) {
        $response["success"] = true;
        echo json_encode($response);
    } else {
        $response["success"] = false;
        echo json_encode($response);
    }

?>

I also tried using PHPMailer but the result was the same, is the problem perhaps not code?

kim
  • 31
  • 5

0 Answers0