Skip Navigation

[Resolved] User to authenticate their email address to send the email from.

This support ticket is created 4 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 21 replies, has 2 voices.

Last updated by nabils 4 years, 4 months ago.

Assisted by: Shane.

Author
Posts
#1689893

Tell us what you are trying to do?
Hi,
I am using user form to register user in my website. In one of the post forms in the website, when the user submit the form an email will be send to an email address in the form (I am using wp_mail php function to send the email). For now the from email is wordpress email because the user email is not authenticated.

$from = get_post_meta( $user_id, "wpv-user_email", true);

How can I authenticate the email address for each user to be the from email?

#1690577

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nabils,

Thank you for getting in touch.

I'm not sure I understand what you mean by authenticate the user's email?

However i'm assuming that you want the email that is sent to be from the User's email and not the default wordpress email on the backend.

Perhaps you can try using the hook in the link below.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_mail_header

This can help you to modify the headers of the email.

Thanks,
Shane

#1691421

Thank you for your reply.

I have added the function to get the header

function customise_cred_notifications( $headers, $formid, $postid, $notification_name, $notification_number ) {
    $myheaders = array();
    $myuser = get_user_by('ID', $postid);
    $myheaders = array( 'Reply-To: '.$myuser->user_email );
    return array_merge($headers, $myheaders);
      
}

Then I have add it in the wp-mail function

  $from = customise_cred_notifications( $headers, $formid, $postid, $notification_name, $notification_number ); 

But still not working. Please let me know if the code I am using is correct.

#1691711

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nabils,

It needs to be added like this.

function customise_cred_notifications( $headers, $formid, $postid, $notification_name, $notification_number ) {
  
    $myheaders = array();
    $myuser = get_user_by('ID', $postid);
    $myheaders = array( 'Reply-To: '.$myuser->user_email );
    return array_merge($headers, $myheaders);
      
}
add_filter('cred_mail_header', 'customise_cred_notifications', 10, 5);

With the add_filter hook. Also is your user currently logged in when they are sending the email ? This code will only get the user's email if they are logged in.

Can you provide a little more information as to where the email is to come from ?

Thanks,
Shane

#1692173

The user will fill a post form with some fields like subject and email content. Then when the user enter submit the email will be send to the email in the parent post. I have used wp-mail function that I have added in the functions.php file.

[php] $status= wp_mail($to, $subject, $mailbody, $from, $attachment);< /code>

What will be $from in this case?

#1692207

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nabils,

This is a completely different case than what this ticket is about.

Your issue is about sending an email to the parent post when you are creating a child post correct?

Please confirm this as I need to exactly what you want to achieve because your initial post is about changing the From parameter to the user's email.

Thanks,
Shane

#1692629

No, my issue is not about sending an email to the parent post. I have already manage to develop the code to send the email to parent post. My issue is exactly as I specified in this ticket.
To make it clear:
What I have achieved:
"To email" -Sending email to post parent when submitting the form. (This is working fine)
What I want to achieve:
"From email"- For now when the email is sent the from email is wordpress. I want to make the from email to be the logged in user email. I think the solution you proposed ( using cred_mail_header)is the correct solution. But I do not know how to added it correctly. Could you please guide me to add the function correctly?

#1692961

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nabils,

Thank you for the clarity.

So here is the correct code to modify the header for the FROM.


function customise_cred_notifications( $headers, $formid, $postid, $notification_name, $notification_number ) {
   
    $myheaders = array();
    $myuser = get_user_by('ID', $postid);
    $myheaders = array( 'From: '.$myuser->user_email );
    return array_merge($headers, $myheaders);
       
}
add_filter('cred_mail_header', 'customise_cred_notifications', 10, 5);

Add it to your Toolset custom codes in Toolset -> Settings -> Custom Code and ensure that you've activated it.

Please try this and let me know if it helps.
Thanks,
Shane

#1704227
pic.png

I have added the function in custom code. Also, I have install smtp plugin for email authentication. However, when I test sending email the email from is showing "the wordpress admin" not the "current logged in user".

#1705289

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nabils,

Would you mind allowing me to have admin access to the site so that I can test this ?

Please let me know the frontend page that you have placed the form on.

Thanks,
Shane

#1706811

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nabils,

Thank you for the credentials.

Just for clarity. The email should be the current user's email that they used when signing up for the site ?

If you go to a user and edit that user. Under Contact Info there is an email field. Is this the email field you want to use ?

Secondly given that you are using the wordpress mail function to send the email it should work once you've let me know where the email field is.

Thanks,
Shane

#1707693

Yes, it is the email field under contact info that I want to use.

#1707843

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nabils,

I've tried to modify your function to include code that should get this done.

What you actually needed was these 2 lines.

$current_user = wp_get_current_user();
 $from = $current_user->user_email;

Just add this to your original function that you made with the wp_mail() function and it should set the $from value to the current user email.

Please let me know if this helps.
Thanks,
Shane

#1708087

I have added the two lines of code in the wp_mail function but still not working.
When WP Mail SMTP plugin is activated it still shows the wordpress admin email as from email. However, when the WP Mail SMTP plugin is not activated it shows the wordpres email as the from email. Please advise.

#1708161

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nabils,

I did some checks on the wordpress docs to see how to modify the header for the FROM section and it should go like this.

$current_user = wp_get_current_user();
 $from = $current_user->user_email;
$headers[] = 'From: '.$from;

wp_mail( $to, $subject, $message, $headers );

So adopt your code to my example above.

Here is the reference document that i've been using to assist you.
https://developer.wordpress.org/reference/functions/wp_mail/

Thanks,
Shane