james123456
New Member
- Messages
- 2
- Reaction score
- 1
- Points
- 3
Hi guys Good Day,
Please help me, I've been struggle for this in 3 days. I didn't receive an email. The Email sending are working properly on my local environment, but when I deployed it in x10hosting then the email are not working.
Here's my code below. Thank you
<?php
namespace App\Services;
// use SendGrid\Mail\Mail;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use Illuminate\Support\Facades\Auth;
use App\Models\EmailLog;
class EmailService
{
public function sendEmail($multiemail, $subject, $content, $cc_emails = null, $from_email = 'janmaegomez040200@gmail.com',$user_id, $content_display, $remarks=null, $status, $url = null, $attachment = null)
{
require '../studentsway/vendor/autoload.php'; // Load Composer's autoloader
$mail = new PHPMailer(true); // Enable PHPMailer exceptions
// Initialize variables
$status_code = 'failed'; // Default status code for failure
$response_body = 'Failed to send email'; // Default message for failure
try {
// SMTP Server settings
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to use
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'janmaegomez******@gmail.com'; // Your Gmail address
$mail->Password = '******'; // Your Google App password (NOT your Gmail password)
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption
$mail->Port = 587; // TCP port to connect to
// Email settings
$fromEmail = $from_email;
$mail->setFrom($fromEmail, 'Studentsway'); // Sender's email and name
$mail->addReplyTo($from_email);
// Add multiple recipients
foreach ($multiemail as $value) {
$mail->addAddress($value);
}
// Add CC emails if provided
if ($cc_emails != null) {
// Convert to array if $cc_emails is a single string
$cc_emails = is_array($cc_emails) ? $cc_emails : [$cc_emails];
foreach ($cc_emails as $value) {
$mail->addCC($value);
}
}
// Email content
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $content;
$mail->AltBody = strip_tags($content); // Plain text version of the email
// // Attach the file if provided
// if ($attachment && file_exists($attachment)) {
// $mail->addAttachment($attachment);
// }
// Add attachment if provided
if ($attachment) {
$mail->addStringAttachment($attachment, 'Scholarship_Application.pdf');
}
// Send the email
$mail->send();
$status_code = 'sent'; // Update status code on success
$response_body = 'Email sent successfully'; // Log success message
} catch (Exception $e) {
$response_body = $mail->ErrorInfo; // Log error message if failed
}
}
Please help me, I've been struggle for this in 3 days. I didn't receive an email. The Email sending are working properly on my local environment, but when I deployed it in x10hosting then the email are not working.
Here's my code below. Thank you
<?php
namespace App\Services;
// use SendGrid\Mail\Mail;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use Illuminate\Support\Facades\Auth;
use App\Models\EmailLog;
class EmailService
{
public function sendEmail($multiemail, $subject, $content, $cc_emails = null, $from_email = 'janmaegomez040200@gmail.com',$user_id, $content_display, $remarks=null, $status, $url = null, $attachment = null)
{
require '../studentsway/vendor/autoload.php'; // Load Composer's autoloader
$mail = new PHPMailer(true); // Enable PHPMailer exceptions
// Initialize variables
$status_code = 'failed'; // Default status code for failure
$response_body = 'Failed to send email'; // Default message for failure
try {
// SMTP Server settings
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to use
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'janmaegomez******@gmail.com'; // Your Gmail address
$mail->Password = '******'; // Your Google App password (NOT your Gmail password)
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption
$mail->Port = 587; // TCP port to connect to
// Email settings
$fromEmail = $from_email;
$mail->setFrom($fromEmail, 'Studentsway'); // Sender's email and name
$mail->addReplyTo($from_email);
// Add multiple recipients
foreach ($multiemail as $value) {
$mail->addAddress($value);
}
// Add CC emails if provided
if ($cc_emails != null) {
// Convert to array if $cc_emails is a single string
$cc_emails = is_array($cc_emails) ? $cc_emails : [$cc_emails];
foreach ($cc_emails as $value) {
$mail->addCC($value);
}
}
// Email content
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $content;
$mail->AltBody = strip_tags($content); // Plain text version of the email
// // Attach the file if provided
// if ($attachment && file_exists($attachment)) {
// $mail->addAttachment($attachment);
// }
// Add attachment if provided
if ($attachment) {
$mail->addStringAttachment($attachment, 'Scholarship_Application.pdf');
}
// Send the email
$mail->send();
$status_code = 'sent'; // Update status code on success
$response_body = 'Email sent successfully'; // Log success message
} catch (Exception $e) {
$response_body = $mail->ErrorInfo; // Log error message if failed
}
}