Send mail issue

Hi all. Recently I’ve migrated my website to another hosting provider and now I’m having problems with sendmail php function. The old hosting server was allowing to send email under any mail account regardless of whether it is present on the server or belongs to the client, using the X-Envelope. Because the previous server configuration method is outdated and poses an increased risk the new Hosting provider doesn’t allow such configuration so the script must be modified in order to send all mail authenticated through the local server. But I know little to nothing PHP, not a web developer, so I really hope you can help me solve this issue. In the functions.php file there are 2 script codes that send email for different purpose, but I’ll post a few code snippets as I don’t know which one should be changed. I need the code bellow to send the mail authenticated, something like:

require_once “Mail.php”;
$username = ‘[email protected]’;
$password = ‘password’;
$smtpHost = ‘127.0.0.1’;
$smtpPort = ‘465’;
$to = ‘[email protected]’;
$from = ‘[email protected]

Code snippet 1:

$emailTo = get_option('admin_email');

$subject = "Order A";

function set_html_content_type() {
		return 'text/html';
}

add_filter( 'wp_mail_content_type', 'set_html_content_type' );
add_filter('wp_mail_from', 'new_mail_from');
add_filter('wp_mail_from_name', 'new_mail_from_name');

function new_mail_from($old) {
		 return 'no-reply@'.$_SERVER['HTTP_HOST'];
}
function new_mail_from_name($old) {
return 'Picky Ins';
}

add_filter( 'wp_mail_content_type', 'set_html_content_type' );

$mail_raion = get_post_meta(  $_POST['raion'], 'region_e-mail' , true );
$emailTo = get_option('admin_email');
$multiple_recipients = array(
$mail_raion,
$emailTo
);

Code snippet 2:

 $emailTo = $transaction->email;

        $subject = "New Order";

        add_filter( 'wp_mail_content_type', function() {
            return 'text/html';
        });

        add_filter('wp_mail_from', function() {
            return 'no-reply@'.$_SERVER['HTTP_HOST'];
        });

        add_filter('wp_mail_from_name', function() {
            return 'Picky Ins';
        });

        $text = "<b>Hello,</b><br/>
        <p>Thank you...Text</p><br/>
        <b>Nice trip</b>";
        $attachments = array(
            'pdf_file/'.$name,
            get_attached_file(1715)
        );
        function set_html_content_type() {
            return 'text/html';
        }
        add_filter( 'wp_mail_content_type', 'set_html_content_type' );
        wp_mail($emailTo, $subject, $text, null, $attachments);
        wp_mail( '[email protected]', $subject, $text, null, $attachments);
        remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
        if ($from_callback) {
            wp_redirect(get_permalink(1620).'?key='.$name);
        }
    }
    elseif($response['RESULT'] == 'OK' && $transaction->type == 1)
    {
        if ($from_callback) {
            wp_redirect(get_permalink(2830));
        }
    }
    else
    {
        if ($from_callback) {
            $data_trans['post_title'] = 'Error';
            $data_trans['set_html_content_type'] = 'YES';
            sendMailAchitaPolitadeAsigurare($data_trans,$transaction->trans_id);
            wp_redirect(get_permalink(2876).'?transaction_error');
        }
    }
}

Code snippet 3:

 add_post_meta( $postId, '_trans_id',$_trans_id, true );


    $subject = $_data['post_title'];

    if(isset($_data['set_html_content_type'])) {
        function set_html_content_type() {
            return 'text/html';
        }
    }


    add_filter( 'wp_mail_content_type', 'set_html_content_type' );
    add_filter('wp_mail_from', 'new_mail_from');
    add_filter('wp_mail_from_name', 'new_mail_from_name');

    function new_mail_from($old) {
        return 'no-reply@'.$_SERVER['HTTP_HOST'];
    }
    function new_mail_from_name($old) {
        return 'Picky Ins';
    }

    add_filter( 'wp_mail_content_type', 'set_html_content_type' );

    $emailTo1 = '[email protected]';
    $multiple_recipients = array(
        $emailTo1
    );

//    wp_mail($multiple_recipients, $subject, $text);

    sendEmailSimple($emailTo1, $subject, 'no-reply@'.$_SERVER['HTTP_HOST'], 'Picky Ins', '', $text);

    remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
}

Thank you in advance.

It seems like you need to use an SMTP plugin or library to send your emails through your new host. PHPMailer is a popular library that supports sending email via SMTP with authentication.

First, you need to install PHPMailer. If your hosting allows it, you can install it with Composer. If you can’t use Composer, you can manually download the PHPMailer from its GitHub repository.

If you can use Composer:

composer require phpmailer/phpmailer

You can use PHPMailer to send an email like this:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require '/path/to/vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      
    $mail->isSMTP();                                           
    $mail->Host       = '127.0.0.1';                    
    $mail->SMTPAuth   = true;                                  
    $mail->Username   = '[email protected]';                     
    $mail->Password   = 'password';                              
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         
    $mail->Port       = 465;                                    

    //Recipients
    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Joe User');     

    //Content
    $mail->isHTML(true);                                  
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

Replace your wp_mail() function calls in your existing PHP scripts with this new PHPMailer code. Make sure to replace the setFrom() , addAddress() , Subject , and Body parameters with the appropriate values from your script. Also, remember to replace the ‘Host’, ‘Username’, and ‘Password’ parameters with your actual SMTP server’s details.

good luck

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service