You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have installed PHPMailer on my website and can receive an email from a form. However, after many hours of trying and searching the internet I have been unable to put the email address that was entered in the form into either the "From" or "Reply-To" fields of the email. I wish to do this so that the recipient of the email can just hit "Send Reply" without having to paste the email address.
Here is my code (sanitized):
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.xxxxx.net'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info@mywebsite.co.uk'; // SMTP username
$mail->Password = '******'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, [ICODE]ssl[/ICODE] also accepted
$mail->Port = 587; // TCP port to connect to
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$body = "You have been contacted by:" ."
". $email ." ". $name ."
". "The message:"."
" . $message ;
$mail->Body = $body;
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Email = $email;
//$mail->setFrom = ($from,$name);
$mail->Name = $name;
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody ='This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
I think that a simple tutorial on creating emails from form data would be appreciated, not only by me, but by others with no knowledge of PHP.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have installed PHPMailer on my website and can receive an email from a form. However, after many hours of trying and searching the internet I have been unable to put the email address that was entered in the form into either the "From" or "Reply-To" fields of the email. I wish to do this so that the recipient of the email can just hit "Send Reply" without having to paste the email address.
Here is my code (sanitized):
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.xxxxx.net'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info@mywebsite.co.uk'; // SMTP username
$mail->Password = '******'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, [ICODE]ssl[/ICODE] also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients'
$mail->setFrom('info@mywebsite.co.uk');
$mail->addAddress('info@mywebsite.co.uk'); // Add a recipient
//$mail->addAddress('info@example.com', 'Information'); // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('me@somedomain.com');
//$mail->addBCC('webmaster@anotherdomain.com');
// Attachments
//$mail->addAttachment('/home/cpanelusername/attachment.txt'); // Add attachments
//$mail->addAttachment('/home/cpanelusername/image.jpg', 'new.jpg'); // Optional name
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$body = "You have been contacted by:" ."
". $email ."
". $name ."
". "The message:"."
" . $message ;
$mail->Body = $body;
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Email = $email;
//$mail->setFrom = ($from,$name);
$mail->Name = $name;
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody ='This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
I think that a simple tutorial on creating emails from form data would be appreciated, not only by me, but by others with no knowledge of PHP.
Beta Was this translation helpful? Give feedback.
All reactions