im am trying to send emails from my wordpress site using the wp_mail function, however my Inbox remains empty . I am pretty much a beginner. If you know what i am doing worng I would appreciate it if you could help onto the right path.
The code runs on Wordpress
I dont get any errors if I execute it
I have the following code:
In my Index.php file I would like to send the mail upon hitting the following button:
<button class="btn btn-primary px-4 btn-lg" onclick="<?php my_function(); ?>" id="btn-s"> Angebot anfordern</button>
In my functions.php file I have the following 2 functions:
add_action( 'phpmailer_init', 'my_phpmailer' );
function my_phpmailer( $phpmailer ) {
$phpmailer->isSMTP();
$phpmailer->Host = SMTP_HOST;
$phpmailer->SMTPAuth = SMTP_AUTH;
$phpmailer->Port = SMTP_PORT;
$phpmailer->Username = SMTP_USER;
$phpmailer->Password = SMTP_PASS;
$phpmailer->SMTPSecure = SMTP_SECURE;
$phpmailer->From = SMTP_FROM;
$phpmailer->FromName = SMTP_NAME;
}
function my_function() {
$to = '[email protected]';
$subject = 'Here is the subject';
$message = 'This is the HTML message body <b>in bold!</b>';
wp_mail( $to, $subject, $message );
}
The SMTP variables are located in my wp-config.php file
The data regarding the gmail server should be valid
These to lines may be relevant
define ('SMTP_PORT','587');
define ('SMTP_SECURE','tls');
I assume there is an error in the first function in the functions.php.
Thank you in advance for any feedback.
Unfortunately since I coded the contact form directly into the Theme - since its only one site and I wanted to use bootstrap - I cant use contact form plugins. I tried utilizing 'phpmailer', which a couldnt get to work either. Though I feel there isnt much missing in my current attempt.
your php function is firing on page load. after that it wont get called.
I would suggest you do this via ajax:
define your ajax url on your page via your
enqueued script
:then define the ajax function:
then in your js: