Sending mail using CDO JavaScript error - Server is undefined - javascript

I got this code to send email using SMTP server, I tried many configuration of it that I found online, also VBscript similar code, and non of it is working.
I want to focus on this code, when I'm opening the HTA I'm getting error in line 8, says 'Server is undefined', What should I do to define it?
var cdoConfig = Server.CreateObject("CDO.Configuration");
cdoConfig.Fields("cdoSMTPServerName") = "194.90.9.22";
var cdoMessage = Server.CreateObject("CDO.Message");
cdoMessage.Configuration = cdoConfig;
var cdoBodyPart = cdoMessage.BodyPart;
cdoMessage.To = "aaa#gmail.com";
cdoMessage.From = "xxx#ddd.com";
cdoMessage.Subject = "CDO Test in JScript";
cdoMessage.TextBody = "This is a test email sent using JScript.";
cdoMessage.send();
Thanks,
Rotem

Jan, you should see that code uses "Server" (VBS), is it recommended ¿?
You may check this:
Your server(running the code) is not allowed to send (mail forwarding).
Auth, .Item(cdoSMTPAuthenticate) = cdoAnonymous, you should see if there's some auth on your mail server.
Hope this helps,

sorry for the delayed response looks like instead of using the server ip you need to spicify the server address for example smptout.secureserver.net but replace with your smpt address also most smtp server require username and password you will need to define these in your code.
in vbs it would be defined like so
Set emailConfig = emailObj.Configuration
emailConfig.Fields("smtpserver") = "your smtp address"
emailConfig.Fields("smtpserverport") = your smpt port
emailConfig.Fields("sendusing")= 2
emailConfig.Fields("smtpauthenticate") = 1
emailConfig.Fields("smtpusessl") = true/false "used for ssl"
emailConfig.Fields("sendusername") = "your smtp username"
emailConfig.Fields("sendpassword") = "your smtp password"
emailConfig.Fields.Update
i do not know js very well so i am unable to convert the vbs code to work in js. i hope this is some help

Related

PHP Mailer returns PHP Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not authenticate

I saw many similar questions, like my, but never the one, which helps me. I am trying PHPMailer from https://github.com/PHPMailer/PHPMailer. I was testing it with xampp server and everything worked fine. But when I uploaded whole phpMailer on my page, some issues appeared. In my log file, I am still getting this error:
PHP Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not authenticate. in /web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php:2089
Stack trace:
#0 web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php(1900): PHPMailer\PHPMailer\PHPMailer->smtpConnect()
#1 web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php(1614): PHPMailer\PHPMailer\PHPMailer->smtpSend()
#2 web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php(1447): PHPMailer\PHPMailer\PHPMailer->postSend()
#3 web/test/idk.php(31): PHPMailer\PHPMailer\PHPMailer->send()
#4 {main}
thrown in web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php on line 2089`
Does anybody know, how to fix this? I am pretty sure my log in credentials are OK. The weird thing is that it is working on xampp fine, but not on my page. I am using the newest PHPMailer, and I didnt edited anything, so you can see the code on PHPMailer github.
If someone needs additinal information, just let me know.
Thank you so much!
EDIT: Here is my idk.php file (the code snippet didn't worked fine, but I think you can read it):
<?php
declare( strict_types = 1 );
// Check if the form is submitted
if ( isset( $_POST['submit'] ) ) {
// retrieve the form data by using the element's name attributes value as key
$subject = $_REQUEST['subject'];
$body = $_REQUEST['body'];
$sender = $_REQUEST['sender'];
require __DIR__ . '/vendor/autoload.php';
$mailer = new \PHPMailer\PHPMailer\PHPMailer( true );
//$mailer->SMTPDebug = \PHPMailer\PHPMailer\SMTP::DEBUG_SERVER tento řádek povoluje client -> server a server -> client zprávy
$mailer->isSMTP();
$mailer->Host = 'smtp.gmail.com';
$mailer->SMTPAuth = true;
$mailer->Username = 'my#mail.com';
$mailer->Password = 'myPassword';
$mailer->SMTPSecure = \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
$mailer->Port = 587;
$mailer->setFrom( 'my#mail.com' );
$mailer->addReplyTo( 'my#mail.com' );
$mailer->addAddress( $sender );
$mailer->isHTML( true );
$mailer->Subject = $subject;
$mailer->Body = $body;
$mailer->send();
}
?>
Maybe I should say that I am using .htaccess file on my web hosting. I am not sure, but maybe this is the reason, why I am getting those errors. When I am trying to access the idk.php file, I am getting http error 500. I did some research and there is some connection beetween this 500 error and htaccess file.
EDIT:
I turned off less secure apps, but it still does not work. I changed port to 465 and now the error in on line 2076 in this file: https://github.com/PHPMailer/PHPMailer/blob/master/src/PHPMailer.php
This is gmail issue, you need Turn off "Less secure app access"
More info:
https://support.google.com/accounts/answer/6010255?hl=en#zippy=%2Cif-less-secure-app-access-is-on-for-your-account%2Cif-less-secure-app-access-is-off-for-your-account
Turn off "Less secure app access"
https://myaccount.google.com/lesssecureapps
Example (PHPMailer v6.2.0)
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
* This uses traditional id & password authentication - look at the gmail_xoauth.phps
* example to see how to use XOAUTH2.
* The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
*/
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require 'vendor/autoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// SMTP::DEBUG_OFF = off (for production use)
// SMTP::DEBUG_CLIENT = client messages
// SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption mechanism to use - STARTTLS or SMTPS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = 'CHANGEME';
//Password to use for SMTP authentication
$mail->Password = 'CHANGEME';
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('CHANGEME', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML('This is a plain-text message body');
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
//Section 2: IMAP
//Uncomment these to save your message in the 'Sent Mail' folder.
#if (save_mail($mail)) {
# echo "Message saved!";
#}
}
Taken from https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
I see you are using Google SMTP. Try enabling “less secure apps” in your Google account. Also make sure you have added your email address the “allowed senders” list.
As this was working on your local machine and not your server, you may also want to add your server IP to the allowed connections.
I’m not sure you need the leading backslash here:
$mailer = new \PHPMailer\PHPMailer\PHPMailer( true );
Or here:
$mailer->SMTPSecure = \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;

GmailApp per email recipient quota or how can I test GmailApp code without sending an email to 50+ people?

Google's quota for recipients per email is unclear. It says 50 here. It says 100, here.
My current code uses MailApp and indeed it seems to fail with over 50 bcc'd:
function sendEmail(form) {
var ss = SpreadsheetApp.getActive();
var body = form.body;
var bcc = form.bccfield;
var cc = form.ccfield;
var to = form.tofield;
var subject = form.subject;
var eHandle = ss.getSheetByName("Email Handling");
var sig = eHandle.getRange(10, 2).getValue();
var img = eHandle.getRange(11, 2).getValue();
var sigImage = '<img src ="' + img + '" >';
MailApp.sendEmail({
to: to,
cc: cc,
bcc: bcc,
subject: subject,
htmlBody: body.replace(/\n/g, '<br>') + sig + sigImage});
}
I am considering switching to using GmailApp per the suggestion here.
Will that get me up to the 100? A user here suggested maybe but it doesn't seem resolved.
I would test it myself but how can I do that without actually trying to send an email to more than 50 people?
Since both of the previous answers are correct for your question I think bundling them into this community wiki is a good thing.
GmailApp & MailApp are Apps Script services, that are different from the Gmail API.
You can also use Gmail API from Apps Script by enabling it by activating "Gmail" in the Advanced Services.
The quotas you mention each refer to a different service.
For testing without spamming (other than yourself) you can use aliases. For example if your email address is: myname#gmail.com then you can make alias addresses by adding a slug in the following way: myname+{text}#gmail.com where "{text}" is a placeholder.
The MailApp and GmailApp services aren't the same as the Gmail API
If you want to use the Gmail API quota in Google Apps Script instead of using MailApp / GmailApp you should use the Gmail Advanced Service.
Resources
https://developers.google.com/apps-script/guides/services/advanced
How can I [test] that without actually trying to send an email to more than 50 people?
You can use email aliases to send the 50 emails to yourself:
If your email address is, for example, myname#gmail.com, then any address of the form
myname+{text}#gmail.com
will automatically route to your own inbox.
So you can use your script to generate a list of aliases, such as:
myname+1#gmail.com, myname+2#gmail.com, ...
and test for any number of email addresses without spamming anyone (well, except yourself... you'll receive 50 copies of that email).

Not getting Confirmation Code on users email

I'm using Registration Form from the link below.
https://github.com/simfatic/RegistrationForm/tree/master/source
The only problem I'm facing is that I'm not getting the confirmation code/link on my(user) email ID.
On my other(admin) email ID, I'm getting the mail that a new user has registered. Even in my SQL, I'm receiving all the information like username, password and confirmation code. But it is not sending the confirmation code/link to the user.
Please Help.
Thank you
I just had a look through the code and it looks fine, you need to double check you corrected all the placeholders correctly, it might be you forgot to change one of them so now it can't push data to the users email.
I can't see what you changed the code to so unable to help you much more.
#AnonyMate
Emails can be sent when it's the live server.
BUT if you want to set email working facility in localhost, then you have to set SMTP configuration as below.
NOTE:- This is just basic setup for SMTP configuration. Different Email libraries may have different syntax.
$mailconfig['charset'] = 'utf-8';
$mailconfig['mailtype'] = 'html';
$mailconfig['newline'] = "\r\n";
if($_SERVER['HTTP_HOST']=="localhost")
{
//SMTP configuration
$mailconfig['protocol'] = 'smtp';
$mailconfig['smtp_host'] = 'ssl://smtp.gmail.com';
$mailconfig['smtp_port'] = 465;
$mailconfig['smtp_user'] = 'your Gmail Email-ID';
$mailconfig['smtp_pass'] = 'your Gmail Email-password';
}
So in your project, please refer source/include/class.phpmailer.php, change SMTP settings accordingly. Also find on which page there is an "email send" functionality. And set above variables values on that page as per that syntax.
EDIT:- Found the page on which there is an email send functionality.
/source/include/fg_membersite.php
In above file, in this function SendUserWelcomeEmail(&$user_rec)
Also in this file /source/include/class.phpmailer.php, make below changes.
NOTE :- Dont't delete old data. just comment it.
//var $Mailer = 'mail';
var $Mailer = 'smtp';
//var $Host = 'localhost';
var $Host = 'ssl://smtp.gmail.com';
//var $Port = 25;
var $Port = 465;
var $SMTPSecure = "ssl";
var $Username = 'your Gmail Email-ID';
var $Password = 'your Gmail Email-password';

How to send email using Javascript in a simplest way?

I'm doing the final project in my XML & JavaScript course and I'm having trouble with sending email via JavaScript.
I found this page: http://smtpjs.com/. But it requires SMTP credentials, I don't know what SMTP credentials is.
Who has used smtpjs.com or who has the solution please help me.
P/s: Do I need to upload my code to a host to send email. I always open html file from my computer, I wonder whether it will be able to send email or not.
First of all, I don't think you should run your code from the html file. You run it from a server to properly communicate with smtp.js
You can try this:
$('#form-submit').click(function () {
submitForm();
});
function submitForm() {
// Initiate Variables With Form Content
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();
if (name == "") {
alert('Invalid Name');
return;
}
if (message == "") {
alert('Message cannot be empty');
return;
}
var emailReg = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/;
if (!emailReg.test(email)) {
alert('Invalid Email');
return;
}
Email.send("from#email.com",
"to#email.com",
"Message from " + name + " " + email,
message,
{
token: "63cb3a19-2684-44fa-b76f-debf422d8b00",
callback: function done(message) { alert("sent") }
});
}
I also used smtpjs and sendgrid.
I hope this helps.
If you want to send an email in javascript, you should turn on allow secure app access in Gmail.
Google will not let you send emails if you do not turn on allow the less secure app in Gmail.
Here is the link to:
Turn On Allow Secure App Access
Please note if you turn on the function in google, it is easy for attackers to find information about your Gmail account.
It is recommended to create an account just for development.
SMTP is the server that handles mails. Here is google's: 'smtp.gmail.com'.
You can directly send an e-mail from javascript. I suggest you go for JSON.

Send email in HTA using JavaScript and hide the sender's email address

I'm using an HTA at work with many options, now I'm trying to add a page that allows you to send an email directly from the HTA without opening MS Outlook.
I'm using Outlook 2003. I tried two ways to create the email sending page:
1. Using Outlook.Application ActiveX Object - It didn't work because its seems to work only with Outlook 2007, So meanwhile I left it out of the question.
2. Using simple HTML with 'mailto:' - It is working fine to send simple Emails, but I have a problem that I'm not able to solve.
In Outlook I can send emails from a 'fake' address called 'Service Mail' ( I just write it in the 'From' field ) so customers won't be able to reply to my emails. I want to do it also in my HTA page, but I think this option doesn't exist.
Is there any way doing it? Maybe by using an ActiveX Object for outlook 2003 and do it with that object?
Important: I can only use client side languages, because I don't have a server.
Thanks,
Rotem
I've made a HTA in VBScript that sends email. It connects directly to the mail server. You don't need Outlook (or any other email client) installed so it's pretty useful. Use something like this:
With CreateObject("CDO.Message")
.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.server.com"
.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Subject = "The subject line"
.To = "recipient#email.com"
.From = "sender#email.com"
.TextBody = "The body of the email"
' or .CreateHTMLbody "page.htm"
.AddAttachment "C:\path\to\file.txt"
.Send
End With
... you get the idea.
Edit: Just saw this request was specifically for javascript, but it's essentially the same:
var mailobj = Server.CreateObject("CDO.Message");
mailobj.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.server.com";
mailobj.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2;
mailobj.Subject = "The subject line";
mailobj.To = "recipient#email.com";
mailobj.From = "sender#email.com";
mailobj.TextBody = "The body of the email";
mailobj.Configuration.Fields.Update();
mailobj.Send();

Categories