本文将为您提供关于有没有一种快速的方法可以在不事先知道文件类型的情况下使用SMTP在python中发送电子邮件附件?的详细介绍,同时,我们还将为您提供关于(PHP初学者帮助)如果您打算使用SMTP,请
本文将为您提供关于有没有一种快速的方法可以在不事先知道文件类型的情况下使用 SMTP 在 python 中发送电子邮件附件?的详细介绍,同时,我们还将为您提供关于(PHP 初学者帮助) 如果您打算使用 SMTP,请在此行之后添加您的 SMTP 代码、2021-05-21 08:29:01 SMTP 错误:无法连接到服务器:网络无法访问101,SMTP 连接失败、Django rest auth :: smtp.EmailBackend:如果在注册期间与 smtp 的连接失败,如何获取一些消息,而不是在调试模式下至少 500、javax.mail.MessagingException:无法连接到 SMTP 主机:
- 有没有一种快速的方法可以在不事先知道文件类型的情况下使用 SMTP 在 python 中发送电子邮件附件?
- (PHP 初学者帮助) 如果您打算使用 SMTP,请在此行之后添加您的 SMTP 代码
- 2021-05-21 08:29:01 SMTP 错误:无法连接到服务器:网络无法访问101,SMTP 连接失败
- Django rest auth :: smtp.EmailBackend:如果在注册期间与 smtp 的连接失败,如何获取一些消息,而不是在调试模式下至少 500
- javax.mail.MessagingException:无法连接到 SMTP 主机:
,端口:25,响应:421
有没有一种快速的方法可以在不事先知道文件类型的情况下使用 SMTP 在 python 中发送电子邮件附件?
如何解决有没有一种快速的方法可以在不事先知道文件类型的情况下使用 SMTP 在 python 中发送电子邮件附件?
我正在开发一个 Python 程序,该程序将使用 SMTP 发送带有附件的电子邮件。到目前为止,在互联网上搜索,我遇到了两种将文件附加到 MIME 电子邮件的主要方法。一种方法是使用 MIMEBase
类并像这样附加文件(无论文件类型如何):
attach_file = open(attach_file_name,''rb'') # Open the file as binary mode
payload = MIMEBase(''application'',''octate-stream'')
payload.set_payload((attach_file).read())
encoders.encode_base64(payload) #encode the attachment
attach_file.close()
#add payload header with filename
payload.add_header(''Content-Decomposition'',"attachment; filename= %s" % attach_file_name)
msg.attach(payload)
这种方法的问题在于,我发现在许多电子邮件客户端(例如 Gmail)上,附件显示为“noname”并且不可预览(尽管您仍然可以通过下载并使用正确的程序打开来查看它) ).
我遇到的第二种方法是附加文件及其类型对应的子类,如下所示:
f=open(attach_file_name,''rb'')
img_data = f.read()
f.close()
image = MIMEImage(img_data,name=os.path.basename(attach_file_name))
msg.attach(image)
此示例适用于图像,并且可以在 Gmail 中正确显示,但不能用于发送其他类型的文件。如果我们想使用方法 2 发送文件,似乎我们首先需要知道它的类型。有没有一种方法可以轻松确定文件的类型,因为它涉及 MIME,以便代码可以即时执行此操作(无需枚举特定文件类型的所有不同扩展名)?或者有没有办法发送附件并让它在不知道其类型的情况下正确显示?
解决方法
SMTP 不关心附件包含什么。只需将其设为 application/octet-stream
并希望收件人知道如何处理它。但请确保正确拼写 MIME 类型。 (“八位字节”只是表示“8 位字节”的一种奇特方式。)
此外,Content-Decomposition
不是有效的 MIME 标头名称;你想要Content-Disposition
。
这些可怕的拼写错误可能是您在某些客户端中发现问题的原因。当您拼错关键技术关键字时,他们无法猜测您的意思。
更根本的是,您的两次尝试似乎都复制/粘贴了几年前的电子邮件代码。当前的 Python 3.6+ email
库代码更具可读性和通用性。可能会扔掉你拥有的东西,然后重新开始 examples in the documentation.
如果您可以正确地猜测您正在传输的文件的 MIME 类型,那么将该信息添加到 MIME 标头中将很有用;但是针对任意附件类型自动执行此操作不太可能有帮助。如果你的猜测逻辑猜错了,那你就声称自己知道什么时候不知道,只会让事情变得更难。
所以,我什至不特别推荐尝试;但是,如果您想亲自验证这是徒劳的,请尝试 mimetypes.guess_type
method from the standard library. 文档表明这仅包含对文件扩展名集合的预设猜测;更彻底的方法是使用文件的实际内容和 libmagic
或类似内容来尝试识别它是什么。
(PHP 初学者帮助) 如果您打算使用 SMTP,请在此行之后添加您的 SMTP 代码
如何解决(PHP 初学者帮助) 如果您打算使用 SMTP,请在此行之后添加您的 SMTP 代码?
我是 PHP 和 HTML 的新手,需要有关使用 PHP 发送电子邮件的帮助。代码我使用的是带有详细记录代码的 Web 模板,这可能是一个简单的修复。
问题:当我在 Live Server 中为联系人表单发送测试电子邮件并按“发送”时,没有任何反应,没有电子邮件通过,也没有成功消息。我的 PHP 代码中是否缺少某些内容,还是需要设置 SMTP?
*我的知识很基础,所以这个问题可能会问错。感谢您的任何反馈!
<?PHP
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require ''PHPmailer/src/Exception.PHP'';
require ''PHPmailer/src/PHPMailer.PHP'';
// If you intend you use SMTP,uncomment next line
require ''PHPmailer/src/SMTP.PHP'';
// Set the recipient email address here
$recipients = array();
$recipients[] = array(
''email'' => ''contact@westonborst.com'',''name'' => ''Weston''
);
// Set the sender email address here
$sender = array(
''email'' => ''contact@westonborst.com'',''name'' => ''Weston''
);
// reCaptcha Secret Key - Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = '''';
// PHPMailer Initialization
$mail = new PHPMailer();
// If you intend you use SMTP,add your SMTP Code after this Line
// End of SMTP
// Form Messages
$message = array(
''success'' => ''Thank you for your message. It has been sent.'',''error'' => ''There was an error trying to send your message. Please try again later.'',''error_bot'' => ''Bot Detected! Message Could not be send. Please try again.'',''error_unexpected'' => ''There was an unexpected error trying to send your message. Please try again later.'',''recaptcha_invalid'' => ''Captcha not Validated! Please Try Again.'',''recaptcha_error'' => ''Captcha not Submitted! Please Try Again.''
);
// Form Processor
if( $_SERVER[''REQUEST_METHOD''] == ''POST'' ) {
$prefix = !empty( $_POST[''prefix''] ) ? $_POST[''prefix''] : '''';
$submits = $_POST;
$botpassed = false;
$message_form = !empty( $submits[''message''] ) ? $submits[''message''] : array();
$message[''success''] = !empty( $message_form[''success''] ) ? $message_form[''success''] : $message[''success''];
$message[''error''] = !empty( $message_form[''error''] ) ? $message_form[''error''] : $message[''error''];
$message[''error_bot''] = !empty( $message_form[''error_bot''] ) ? $message_form[''error_bot''] : $message[''error_bot''];
$message[''error_unexpected''] = !empty( $message_form[''error_unexpected''] ) ? $message_form[''error_unexpected''] : $message[''error_unexpected''];
$message[''recaptcha_invalid''] = !empty( $message_form[''recaptcha_invalid''] ) ? $message_form[''recaptcha_invalid''] : $message[''recaptcha_invalid''];
$message[''recaptcha_error''] = !empty( $message_form[''recaptcha_error''] ) ? $message_form[''recaptcha_error''] : $message[''recaptcha_error''];
// Bot Protection
if( isset( $submits[ $prefix . ''botcheck'' ] ) ) {
$botpassed = true;
}
if( !empty( $submits[ $prefix . ''botcheck'' ] ) ) {
$botpassed = false;
}
if( $botpassed == false ) {
echo ''{ "alert": "error","message": "'' . $message[''error_bot''] . ''" }'';
exit;
}
// reCaptcha
if( isset( $submits[''g-recaptcha-response''] ) ) {
$recaptcha_data = array(
''secret'' => $recaptcha_secret,''response'' => $submits[''g-recaptcha-response'']
);
$rc_verify = curl_init();
curl_setopt( $rc_verify,CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify" );
curl_setopt( $rc_verify,CURLOPT_POST,true );
curl_setopt( $rc_verify,CURLOPT_POSTFIELDS,http_build_query( $recaptcha_data ) );
curl_setopt( $rc_verify,CURLOPT_SSL_VERIFYPEER,false );
curl_setopt( $rc_verify,CURLOPT_RETURNTRANSFER,true );
$rc_response = curl_exec( $rc_verify );
$g_response = json_decode( $rc_response );
if ( $g_response->success !== true ) {
echo ''{ "alert": "error","message": "'' . $message[''recaptcha_invalid''] . ''" }'';
exit;
}
}
$html_title = !empty( $submits[''html_title''] ) ? $submits[''html_title''] : ''Form Response'';
$forcerecaptcha = ( !empty( $submits[''force_recaptcha''] ) && $submits[''force_recaptcha''] != ''false'' ) ? true : false;
$replyto = !empty( $submits[''replyto''] ) ? explode( '','',$submits[''replyto''] ) : false;
if( $forcerecaptcha ) {
if( !isset( $submits[''g-recaptcha-response''] ) ) {
echo ''{ "alert": "error","message": "'' . $message[''recaptcha_error''] . ''" }'';
exit;
}
}
$mail->Subject = !empty( $submits[''subject''] ) ? $submits[''subject''] : ''Form response from your website'';
$mail->SetFrom( $sender[''email''],$sender[''name''] );
if( !empty( $replyto ) ) {
if( count( $replyto ) > 1 ) {
$replyto_e = $submits[ $replyto[0] ];
$replyto_n = $submits[ $replyto[1] ];
$mail->AddReplyTo( $replyto_e,$replyto_n );
} elseif( count( $replyto ) == 1 ) {
$replyto_e = $submits[ $replyto[0] ];
$mail->AddReplyTo( $replyto_e );
}
}
foreach( $recipients as $recipient ) {
$mail->AddAddress( $recipient[''email''],$recipient[''name''] );
}
$unsets = array( ''prefix'',''subject'',''replyto'',''message'',$prefix . ''botcheck'',''g-recaptcha-response'',''force_recaptcha'',$prefix . ''submit'' );
foreach( $unsets as $unset ) {
unset( $submits[ $unset ] );
}
$fields = array();
foreach( $submits as $name => $value ) {
if( empty( $value ) ) continue;
$name = str_replace( $prefix,'''',$name );
$name = ucwords( str_replace( ''-'','' '',$name ) );
if( is_array( $value ) ) {
$value = implode( '',$value );
}
$fields[$name] = $value;
}
$response = array();
foreach( $fields as $fieldname => $fieldvalue ) {
$response[] = $fieldname . '': '' . $fieldvalue;
}
$referrer = $_SERVER[''HTTP_REFERER''] ? ''<br><br><br>This Form was submitted from: '' . $_SERVER[''HTTP_REFERER''] : '''';
$body = implode( "<br>",$response ) . $referrer;
$mail->MsgHTML( $body );
$sendEmail = $mail->Send();
if( $sendEmail == true ):
if( $autores && !empty( $replyto_e ) ) {
$send_arEmail = $autoresponder->Send();
}
echo ''{ "alert": "success","message": "'' . $message[''success''] . ''" }'';
else:
echo ''{ "alert": "error","message": "'' . $message[''error''] . ''<br><br><strong>Reason:</strong><br>'' . $mail->ErrorInfo . ''" }'';
endif;
} else {
echo ''{ "alert": "error","message": "'' . $message[''error_unexpected''] . ''" }'';
}
?>
解决方法
如果您正确填写了所有内容,这应该可以工作。 (我试过了)
require ''PHPMailer/PHPMailer.php'';
require ''PHPMailer/SMTP.php'';
require ''PHPMailer/Exception.php'';
$mail = new PHPMailer\PHPMailer\PHPMailer();
try {
$mail->IsSMTP(); // enable SMTP
$mail->CharSet = ''UTF-8'';
$mail->SMTPDebug = 2; // debugging: 1 = errors and messages,2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = ''ssl''; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "Your Email Address";
$mail->Password = "Your Password";
$mail->SetFrom("From Name");
$mail->Subject = "Message Subject";
$mail->Body ="Message Body ";
$mail->AddAddress(''Sent email to this address'');
if($mail->Send()) {
// e-posta başarılı ile gönderildi
echo ''success'';
} else {
echo ''error'';
}
} catch (Exception $e) {
echo ''error : ''.$e;
}
如果不是,请分享您收到的错误消息。
,不知道为什么它不会发送,这是我试过的?有什么建议吗? (我从代码中删除了密码)
TypeError: ''Series'' objects are mutable,thus they cannot be hashed
2021-05-21 08:29:01 SMTP 错误:无法连接到服务器:网络无法访问101,SMTP 连接失败
如何解决2021-05-21 08:29:01 SMTP 错误:无法连接到服务器:网络无法访问101,SMTP 连接失败
对不起,我真的需要一些关于这段代码的帮助:
Django rest auth :: smtp.EmailBackend:如果在注册期间与 smtp 的连接失败,如何获取一些消息,而不是在调试模式下至少 500
如何解决Django rest auth :: smtp.EmailBackend:如果在注册期间与 smtp 的连接失败,如何获取一些消息,而不是在调试模式下至少 500
我在 Django 中有一个注册表单
用户在注册时会收到一封电子邮件
我已经在设置中设置了我的电子邮件
EMAIL_BACKEND = ''django.core.mail.backends.smtp.EmailBackend''
EMAIL_HOST = ''smtp.gmail.com''
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_PORT = 587
EMAIL_HOST_USER =EMAIL_HOST_USER
EMAIL_HOST_PASSWORD = PASSWORD
我正在使用 django rest auth 包进行注册。
并为ACCOUNT_EMAIL_VERIFICATION = ''mandatory''
设置django-allauth
如果由于某种原因 Django smtp.EmailBackend
无法连接,我会看到 500
响应
我有时不知道是什么问题。
那么我怎样才能收到一些消息,因为 Django allauth 的电子邮件后端失败,至少在 DEBUG 模式下。
javax.mail.MessagingException:无法连接到 SMTP 主机:,端口:25,响应:421
如何解决javax.mail.MessagingException:无法连接到 SMTP 主机:<smtp 主机>,端口:25,响应:421
我在 Scala 中构建了一个 Spark 应用程序,用于发送警报电子邮件。检查 Yarn 中的日志,一旦我的作业提交,我就会不断收到该错误消息:
"''Patriots'',''whitehouse''"
这让我感到惊讶,因为在 Spark shell 中测试代码时,我能够使用相同的代码行发送电子邮件而不会出现相同的错误。
这是我的代码的样子:
21/06/08 11:11:07,333 INFO DAGScheduler: Job 20 finished: collect at SendingMail.scala:14,took 0.078407 s
javax.mail.MessagingException: Could not connect to SMTP host: <smtp host>,port: 25,response: 421
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1960)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
...
今天关于有没有一种快速的方法可以在不事先知道文件类型的情况下使用 SMTP 在 python 中发送电子邮件附件?的讲解已经结束,谢谢您的阅读,如果想了解更多关于(PHP 初学者帮助) 如果您打算使用 SMTP,请在此行之后添加您的 SMTP 代码、2021-05-21 08:29:01 SMTP 错误:无法连接到服务器:网络无法访问101,SMTP 连接失败、Django rest auth :: smtp.EmailBackend:如果在注册期间与 smtp 的连接失败,如何获取一些消息,而不是在调试模式下至少 500、javax.mail.MessagingException:无法连接到 SMTP 主机:
本文标签: